1.4.2 用LINQ to XML 对 XML 文件进行处理
2012-04-24 23:11
417 查看
<?xml version="1.0" encoding="utf-8" ?> <Data> <Products> <Product Name="West Side Story" Price="9.99" SupplierID="1"></Product> <Product Name="Assassins" Price="14.99" SupplierID="2"></Product> <Product Name="Frogs" Price="13.99" SupplierID="1"></Product> <Product Name="Sweeney Todd" Price="10.99" SupplierID="3"></Product> </Products> <Suppliers> <Sopplier Name="Solely Sondheim" SupplierID="1"></Sopplier> <Sopplier Name="CD-by-CD-by-Sondheim" SupplierID="2"></Sopplier> <Sopplier Name="Barbershop CDs" SupplierID="3"></Sopplier> </Suppliers> </Data>
XDocument doc = XDocument.Load("data.xml"); var filtered = from p in doc.Descendants("Product") join s in doc.Descendants("Sopplier") on (int)p.Attribute("SupplierID") equals (int)s.Attribute("SupplierID") where (decimal)p.Attribute("Price") > 10 orderby (string)s.Attribute("Name"), (string)p.Attribute("Name") select new { SupplierName = (string)s.Attribute("Name"), ProductName = (string)p.Attribute("Name") }; foreach(var v in filtered) { Console.WriteLine("Supplier={0};Product={1}",v.SupplierName,v.ProductName); }
相关文章推荐
- [代码]使用XmlReader对XML片段进行流式处理(LINQ to XML)
- 使用Linq to Xml 读取配置文件
- 使用Linq to Xml 读取配置文件
- linq to xml 简单的增、删、改、查、保存xml文件操作
- ASP.NET WebApi 上传文件时异常 Failed to execute send on XMLHttpRequest 的一个处理方法
- Linq to Oracle 使用教程(五)使用 Xml 映射文件
- “System.NotSupportedException”类型的异常在 System.Data.Entity.dll 中发生,但未在用户代码中进行处理 其他信息: 在 LINQ to Entitie
- Linq to xml 读取xml文件
- [代码]如何从文件加载XML(LINQ to XML)
- LinQ To XML 新建文件并增加节点
- c# comboBox 使用 linq to xml 绑定 XML文件
- 序列化和LINQ to XML处理XML
- 在metro应用中用linq to xml+文件操作生成XML
- Linq to Xml,从其他xml文件读取节点(Element和Text混合)插入当前xml文件, 尖括号不转义
- linq to xml 简单的增、删、改、查、保存xml文件操作
- Linq to xml:流式处理 XmlReader
- LINQ to XML 从逗号分隔值 (CSV) 文件生成 XML 文件
- linq to xml/动态类型 从树型表构建树(linq to entity处理数据库)
- c#中使用linq to xml 访问 xml文件