[代码]使用LINQ的查询结果填充XML树(LINQ to XML)
2010-10-27 18:36
253 查看
此代码使用的是LINQ to XML的函数构造功能创建一个XML树,其中树中的一些元素是通过LINQ查询结果填充。实际上,通过本例也可以看到如何使用LINQ to XML将原始XML文档转换成为另外一种XML文档。
下面代码中使用的PurchaseOrder.xml文档内容:
示例代码
代码中首先将PurchaseOrder.xml文档的内容通过XDocument.Load()方法加载到内存中,然后调用XDocument.Descendants()方法找到元素名为Items的所有元素。接着使用函数构造创建了一个XElement对象,它的内容是由LINQ to XML的查询结果来填充。在查询中,遍历每一个Items元素,然后找出这些元素中名为Item的所有子元素。
运行上面的代码,会在控制台上输出如下的XML内容,很明显这和PurchaseOrder.xml中的格式是有区别的。
下面代码中使用的PurchaseOrder.xml文档内容:
<?xml version="1.0"?> <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20"> <Address Type="Shipping"> <Name>Ellen Adams</Name> <Street>123 Maple Street</Street> <City>Mill Valley</City> <State>CA</State> <Zip>10999</Zip> <Country>USA</Country> </Address> <Address Type="Billing"> <Name>Tai Yee</Name> <Street>8 Oak Avenue</Street> <City>Old Town</City> <State>PA</State> <Zip>95819</Zip> <Country>USA</Country> </Address> <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes> <Items> <Item PartNumber="872-AA"> <ProductName>Lawnmower</ProductName> <Quantity>1</Quantity> <USPrice>148.95</USPrice> <Comment>Confirm this is electric</Comment> </Item> <Item PartNumber="926-AA"> <ProductName>Baby Monitor</ProductName> <Quantity>2</Quantity> <USPrice>39.98</USPrice> <ShipDate>1999-05-21</ShipDate> </Item> </Items> </PurchaseOrder>
示例代码
代码中首先将PurchaseOrder.xml文档的内容通过XDocument.Load()方法加载到内存中,然后调用XDocument.Descendants()方法找到元素名为Items的所有元素。接着使用函数构造创建了一个XElement对象,它的内容是由LINQ to XML的查询结果来填充。在查询中,遍历每一个Items元素,然后找出这些元素中名为Item的所有子元素。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; namespace Demo03Ex01 { class Program { static void Main(string[] args) { XDocument Document = XDocument.Load(@"C:/LINQ/PurchaseOrder.xml"); IEnumerable<XElement> ItemsElements = Document.Descendants("Items"); XElement OrdersElement = new XElement("Orders", from Items in ItemsElements from Item in Items.Descendants("Item") select Item); Console.WriteLine(OrdersElement); } } }
运行上面的代码,会在控制台上输出如下的XML内容,很明显这和PurchaseOrder.xml中的格式是有区别的。
<Orders> <Item PartNumber="872-AA"> <ProductName>Lawnmower</ProductName> <Quantity>1</Quantity> <USPrice>148.95</USPrice> <Comment>Confirm this is electric</Comment> </Item> <Item PartNumber="926-AA"> <ProductName>Baby Monitor</ProductName> <Quantity>2</Quantity> <USPrice>39.98</USPrice> <ShipDate>1999-05-21</ShipDate> </Item> </Orders>
相关文章推荐
- [代码]使用XmlWriter填充XML树(LINQ to XML)
- LINQ TO XML查询结果null 解决方法
- Entity Framework 4 in Action读书笔记——第四章:使用LINQ to Entities查询:投影结果
- [代码]使用XSD验证XML示例(LINQ to XML)(转)
- C#程序中使用LINQ to XML来查询XML格式数据的实例
- [代码]使用XSD验证XML示例1(LINQ to XML)
- VS2008下使用Linq To Entity的Skip().Take()分页查询时遇到数据结果不对的问题
- 使用FOR XML将查询结果以XML格式返回(Using the FOR XML Clause to Return Query Results as XML)
- [代码]使用XSD验证XML示例2(LINQ to XML)
- 使用Python读取TestTrack记录- Part3 动态生成Linq to xml查询语句筛选条件
- linq to sql取出随机记录/多表查询/将查询出的结果生成xml
- Unity上使用Linq To XML——简单易维护的代码
- Linq To Xml 备忘录3(使用Namespace的查询)
- [代码]使用函数构造创建具有属性的元素(LINQ to XML)
- linq to sql取出随机记录/多表查询/将查询出的结果生成xml
- [代码]如何使用为顺序结果形状映射的存储过程(LINQ to SQL)
- [代码]使用函数构造创建XML树(LINQ to XML)
- 编写高质量代码改善C#程序的157个建议——建议26:使用匿名类型存储LINQ查询结果
- C#使用linq to xml完成对XML文件的创建、增加、删除、修改、查询
- [代码]如何使用为多个结果形状映射的存储过程(LINQ to SQL)