步驟一:通過XML獲取XSD
格式:xsd "XML的完整路徑帶文件名" /O:"輸出路徑不帶文件名"。
C:\Windows\system32>xsd "D:\2 Queue_List\QueueList_request.xml" /O:"D:\2 Queue_List"
步驟二:通過XSD獲取Class
xsd file.xsd {/classes | /dataset} [/element:element] [/language:language] [/namespace:namespace] [/outputdir:directory] [URI:uri]
C:\Windows\system32>xsd /c "D:\2 Queue_List\QueueList_request.xsd" /O:"D:\2 Queue_List"
步驟三:通過Class獲取XML
注意推薦把產生的Class中的數組變成List方便使用。
寫XML幫助類
using System.IO;
using System.Text;
using System.Xml.Serialization; namespace XSD
{
/// <summary>
/// XmlHelper 的摘要描述
/// </summary>
public class XmlHelper
{
public XmlHelper()
{
//
// TODO: 在此加入建構函式的程式碼
//
} public string Serialize<T>(T dataToSerialize)
{
try
{
var serializer = new XmlSerializer(typeof(T));
//// 不聲明頭
//XmlWriterSettings settings = new XmlWriterSettings();
//settings.Indent = true;
//settings.NewLineChars = "";
////settings.Encoding = encoding;
////settings.IndentChars = " "; //// 不聲明頭
//settings.OmitXmlDeclaration = true; // 強制指定新的命名空間,這裡設置為空
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add(string.Empty, string.Empty); Utf8StringWriter stringwriter = new Utf8StringWriter();
serializer.Serialize(stringwriter, dataToSerialize, namespaces);
return stringwriter.ToString();
}
catch
{
throw;
}
} public T Deserialize<T>(string xmlText)
{
try
{
var stringReader = new System.IO.StringReader(xmlText);
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(stringReader);
}
catch
{
throw;
}
} public class Utf8StringWriter : StringWriter
{ public override Encoding Encoding
{
get
{
return Encoding.UTF8;
}
}
}
}
}
測試方法
using System;
using System.Collections.Generic;
using System.Text; namespace XSD
{
class Program
{
static void Main(string[] args)
{ Queue_List queueList = new Queue_List();
queueList.Items = new List<object>(); #region queueNumber
Queue_ListQueueNumber qn = new Queue_ListQueueNumber();
qn.queueDetails = new List<Queue_ListQueueNumberQueueDetails>();
//Queue_ListQueueNumberQueueDetails obj = new Queue_ListQueueNumberQueueDetails();
//obj.number = "1";
//qn.queueDetails.Add(obj);
qn.queueDetails.Add(new Queue_ListQueueNumberQueueDetails() { number = "" });
queueList.Items.Add(qn);
#endregion Queue_ListCategoryDetails bb = new Queue_ListCategoryDetails();
bb.subQueueInfoDetails = new List<Queue_ListCategoryDetailsSubQueueInfoDetails>();
//Queue_ListCategoryDetailsSubQueueInfoDetails b11 = new Queue_ListCategoryDetailsSubQueueInfoDetails();
//b11.identificationType = "C";
//b11.itemNumber = "1";
//bb.subQueueInfoDetails.Add(b11);
bb.subQueueInfoDetails.Add(new Queue_ListCategoryDetailsSubQueueInfoDetails() { identificationType = "C", itemNumber = "" });
queueList.Items.Add(bb); #region scanRange
Queue_ListScanRange cc = new Queue_ListScanRange();
cc.rangeQualifier = "";
cc.rangeDetails = new List<Queue_ListScanRangeRangeDetails>();
Queue_ListScanRangeRangeDetails c11 = new Queue_ListScanRangeRangeDetails();
c11.max = "";
c11.min = "";
cc.rangeDetails.Add(c11); queueList.Items.Add(cc);
#endregion //qn.queueDetails = new Queue_ListQueueNumberQueueDetails[1] {Queue_ListQueueNumberQueueDetails}; string strXml = new XmlHelper().Serialize(queueList); Console.WriteLine(strXml);
Console.Read();
}
}
}