Linq To Xml 备忘录2(X* 类的使用)

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>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值