Linq To XML 中使用到的以X開頭的類都在 System.Xml.Linq 命名空間下。如所示:
XObject
|-XAttribute
|-XNode
|-XComment
|-XContainer
| |-XElement
| |-XDocument
|-XDocumentType
|-XProcessingInstruction
|-XText
|-XCData
另外比較有用的類:XStreamElement 用於處理大資料量的XML,以提高效能。
1. XML文檔作成:
利用 XDocument, XElement, XAttribute, XDeclaration 作成XML。
public static void CreateXDocument()<br />{<br /> System.Xml.Linq.XDocument doc = CreateTestXDocument();<br /> doc.Save(@"D:/temp/CreateXDocument.xml");<br /> Console.WriteLine(doc.ToString());<br />}<br />public static XDocument CreateTestXDocument()<br />{<br /> System.Xml.Linq.XDocument doc =<br /> new XDocument(<br /> new XDeclaration("1.0", "UTF-8", "yes"),<br /> new XElement("Product",<br /> new XAttribute("id", "RX78"),<br /> new XAttribute("operator", "amuro"),<br /> new XElement("name", "Gundam"),<br /> new XElement("price", "1000"),<br /> new XElement("material", "gundarium")<br /> )<br /> );<br /> return doc;<br />}
將會輸出下面的結果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?><br /><Product id="RX78" operator="amuro"><br /> <name>Gundam</name><br /> <price>1000</price><br /> <material>gundarium</material><br /></Product>
2. 通過 XNamespace 指定命名空間
public static void TestXNamespace()<br />{<br /> XNamespace ns = "http://ns.handcraft.org/testnamespace";<br /> XElement e = new XElement(ns + "product", new XAttribute("id", "RX78"),<br /> new XElement(ns + "name", "Gundam"), new XElement("append", "nonamespace"));<br /> Console.WriteLine("預設Namespace:");<br /> Console.WriteLine(e); </p><p> Console.WriteLine(e.Name); </p><p> XNamespace ns1 = "http://ns.handcraft.org/testnamespace";<br /> XElement e1 = new XElement(ns1 + "product",new XAttribute(XNamespace.Xmlns + "e1", ns), new XAttribute("id", "RX78"),<br /> new XElement(ns1 + "name", "Gundam"), new XElement("append", "nonamespace"));<br /> Console.WriteLine("先頭追加Namespace:");<br /> Console.WriteLine(e1);<br />}
將會輸出以下結果:
預設Namespace:<br /><product id="RX78" xmlns="http://ns.handcraft.org/testnamespace"><br /> <name>Gundam</name><br /> <append xmlns="">nonamespace</append><br /></product><br />{http://ns.handcraft.org/testnamespace}product<br />先頭追加Namespace:<br /><e1:product xmlns:e1="http://ns.handcraft.org/testnamespace" id="RX78"><br /> <e1:name>Gundam</e1:name><br /> <append>nonamespace</append><br /></e1:product>
3. XPathEvaluate, XPathSelectElements (這些是擴充方法,命名空間:System.Xml.XPath)
通過 XPathEvaluate, XPathSelectElements 使用XPath功能。
下面是樣本的XML:
<?xml version="1.0" encoding="utf-8"?><br /><products><br /> <product id="product1" category="book"><br /> <name>Programing Something</name><br /> <price>2000</price><br /> <publishDate>2008/10/10</publishDate><br /> <authors><br /> <author>Tarou Book</author><br /> <author>Hanako Book</author><br /> </authors><br /> </product><br /> <product id="product2" category="book"><br /> <name>Administrating Something</name><br /> <price>5000</price><br /> <publishDate>2008/10/12</publishDate><br /> <authors><br /> <author>Kotarou Book1</author><br /> <author>Kohanako Book2</author><br /> </authors><br /> </product><br /> <product id="product3" category="novel"><br /> <name>Suspection novel</name><br /> <price>500</price><br /> <publishDate>2007/12/12</publishDate><br /> <authors><br /> <author>Jirou Tarou</author><br /> </authors><br /> </product><br /> <product id="product4" category="novel"><br /> <name>Fantasy novel</name><br /> <price>540</price><br /> <publishDate>2008/9/14</publishDate><br /> <authors><br /> <author>Tarou Book</author><br /> </authors><br /> </product><br /> <product id="product5" category="cram"><br /> <name>Study English</name><br /> <price>2400</price><br /> <publishDate>2008/9/14</publishDate><br /> <authors><br /> <author>English Tarou</author><br /> <author>English Jirou</author><br /> </authors><br /> </product><br /></products><br />
XPathEvalute的使用樣本:
public static void XPathEvaluate()<br />{<br /> XElement products = XElement.Load(@"D:/temp/products.xml");<br /> var result = (IEnumerable<Object>) products.XPathEvaluate("/product[@category='book']/name/text()");<br /> foreach (var item in result)<br /> {<br /> Console.WriteLine(item);<br /> }<br />}
結果如下:
Programing Something
Administrating Something
XPathSelectElements的使用樣本:
public static void XPathSelect()<br />{<br /> XElement products = XElement.Load(@"D:/temp/products.xml");<br /> var query = from c in products.XPathSelectElements("/product[@category='book']")<br /> orderby (string) c.Element("name")<br /> select c;<br /> foreach (var item in query)<br /> {<br /> Console.WriteLine(item);<br /> }<br />}
結果如下:
<product id="product2" category="book"><br /> <name>Administrating Something</name><br /> <price>5000</price><br /> <publishDate>2008/10/12</publishDate><br /> <authors><br /> <author>Kotarou Book1</author><br /> <author>Kohanako Book2</author><br /> </authors><br /></product><br /><product id="product1" category="book"><br /> <name>Programing Something</name><br /> <price>2000</price><br /> <publishDate>2008/10/10</publishDate><br /> <authors><br /> <author>Tarou Book</author><br /> <author>Hanako Book</author><br /> </authors><br /></product>