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() { System.Xml.Linq.XDocument doc = CreateTestXDocument(); doc.Save(@"D:/temp/CreateXDocument.xml"); Console.WriteLine(doc.ToString()); } public static XDocument CreateTestXDocument() { System.Xml.Linq.XDocument doc = new XDocument( new XDeclaration("1.0", "UTF-8", "yes"), new XElement("Product", new XAttribute("id", "RX78"), new XAttribute("operator", "amuro"), new XElement("name", "Gundam"), new XElement("price", "1000"), new XElement("material", "gundarium") ) ); return doc; }
将会输出下面的结果:
<?xml version="1.0" encoding="utf-8" standalone="yes"?> <Product id="RX78" operator="amuro"> <name>Gundam</name> <price>1000</price> <material>gundarium</material> </Product>
2. 通过 XNamespace 指定命名空间
public static void TestXNamespace() { XNamespace ns = "http://ns.handcraft.org/testnamespace"; XElement e = new XElement(ns + "product", new XAttribute("id", "RX78"), new XElement(ns + "name", "Gundam"), new XElement("append", "nonamespace")); Console.WriteLine("默认Namespace:"); Console.WriteLine(e); Console.WriteLine(e.Name); XNamespace ns1 = "http://ns.handcraft.org/testnamespace"; XElement e1 = new XElement(ns1 + "product",new XAttribute(XNamespace.Xmlns + "e1", ns), new XAttribute("id", "RX78"), new XElement(ns1 + "name", "Gundam"), new XElement("append", "nonamespace")); Console.WriteLine("先头追加Namespace:"); Console.WriteLine(e1); }
将会输出以下结果:
默认Namespace: <product id="RX78" xmlns="http://ns.handcraft.org/testnamespace"> <name>Gundam</name> <append xmlns="">nonamespace</append> </product> {http://ns.handcraft.org/testnamespace}product 先头追加Namespace: <e1:product xmlns:e1="http://ns.handcraft.org/testnamespace" id="RX78"> <e1:name>Gundam</e1:name> <append>nonamespace</append> </e1:product>
3. XPathEvaluate, XPathSelectElements (这些是扩展方法,命名空间:System.Xml.XPath)
通过 XPathEvaluate, XPathSelectElements 使用XPath功能。
下面是示例的XML:
<?xml version="1.0" encoding="utf-8"?> <products> <product id="product1" category="book"> <name>Programing Something</name> <price>2000</price> <publishDate>2008/10/10</publishDate> <authors> <author>Tarou Book</author> <author>Hanako Book</author> </authors> </product> <product id="product2" category="book"> <name>Administrating Something</name> <price>5000</price> <publishDate>2008/10/12</publishDate> <authors> <author>Kotarou Book1</author> <author>Kohanako Book2</author> </authors> </product> <product id="product3" category="novel"> <name>Suspection novel</name> <price>500</price> <publishDate>2007/12/12</publishDate> <authors> <author>Jirou Tarou</author> </authors> </product> <product id="product4" category="novel"> <name>Fantasy novel</name> <price>540</price> <publishDate>2008/9/14</publishDate> <authors> <author>Tarou Book</author> </authors> </product> <product id="product5" category="cram"> <name>Study English</name> <price>2400</price> <publishDate>2008/9/14</publishDate> <authors> <author>English Tarou</author> <author>English Jirou</author> </authors> </product> </products>
XPathEvalute的使用示例:
public static void XPathEvaluate() { XElement products = XElement.Load(@"D:/temp/products.xml"); var result = (IEnumerable<Object>) products.XPathEvaluate("/product[@category='book']/name/text()"); foreach (var item in result) { Console.WriteLine(item); } }
结果如下:
Programing Something
Administrating Something
XPathSelectElements的使用示例:
public static void XPathSelect() { XElement products = XElement.Load(@"D:/temp/products.xml"); var query = from c in products.XPathSelectElements("/product[@category='book']") orderby (string) c.Element("name") select c; foreach (var item in query) { Console.WriteLine(item); } }
结果如下:
<product id="product2" category="book"> <name>Administrating Something</name> <price>5000</price> <publishDate>2008/10/12</publishDate> <authors> <author>Kotarou Book1</author> <author>Kohanako Book2</author> </authors> </product> <product id="product1" category="book"> <name>Programing Something</name> <price>2000</price> <publishDate>2008/10/10</publishDate> <authors> <author>Tarou Book</author> <author>Hanako Book</author> </authors> </product>
&spm=1001.2101.3001.5002&articleId=81927986&d=1&t=3&u=5d8e670f6bd44f6795d158888a956da2)
140

被折叠的 条评论
为什么被折叠?



