步驟一:通過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{ ////// XmlHelper 的摘要描述 /// public class XmlHelper { public XmlHelper() { // // TODO: 在此加入建構函式的程式碼 // } public string Serialize(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 (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