data.xml

 <?xml version="1.0" encoding="utf-8" ?>
<Data>
<Products>
<Product Name="West Side Story" Price="9.99" SupplierID="1" />
<Product Name="Assassins" Price="14.99" SupplierID="2" />
<Product Name="Frogs" Price="13.99" SupplierID="1" />
<Product Name="Sweeney Todd" Price="10.99" SupplierID="3" />
</Products> <Suppliers>
<Supplier Name="Solely Sondheim" SupplierID="1" />
<Supplier Name="CD-by-CD-by-Sondheim" SupplierID="2" />
<Supplier Name="Barbershop CDs" SupplierID="3" />
</Suppliers>
</Data>

通过 linq to xml ,查找价格超过10的产品,并打印供应商名称与产品名称;

             XDocument doc = XDocument.Load("data.xml");
var filtered = from p in doc.Descendants("Product")
join s in doc.Descendants("Supplier")
on (int)p.Attribute("SupplierID")
equals (int)s.Attribute("SupplierID")
where (decimal)p.Attribute("Price") >
select new
{
ProductName = (string)p.Attribute("Name"),
SupplierName = (string)s.Attribute("Name")
}; foreach (var v in filtered)
{
Console.WriteLine("SupplierName={0} , ProductName={1}", v.SupplierName, v.ProductName);
}

输出

SupplierName=CD-by-CD-by-Sondheim , ProductName=Assassins
SupplierName=Solely Sondheim , ProductName=Frogs
SupplierName=Barbershop CDs , ProductName=Sweeney Todd

参考资料

1、深入理解C#(第2版);

C# linq to xml 简单示例的更多相关文章

  1. LINQ for XML简单示例

    LINQ,语言集成查询(Language Integrated Query)是一组用于c#和Visual Basic语言的扩展.它允许开发人员以与查询数据库相同的方式操作内存数据.从技术角度而言,LI ...

  2. spring-servlet.xml简单示例

    spring-servlet.xml简单示例 某个项目中的spring-servlet.xml 记下来以后研究用 <!-- springMVC简单配置 --> <?xml versi ...

  3. linq to xml 简单的增、删、改、查、保存xml文件操作

    using System; using System.Collections; using System.Configuration; using System.Data; using System. ...

  4. linq to xml运用示例

    代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  5. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  6. linq to xml操作XML(转)

    转自:http://www.cnblogs.com/yukaizhao/archive/2011/07/21/linq-to-xml.html LINQ to XML提供了更方便的读写xml方式.前几 ...

  7. C# 构建XML(简单示例)

    C# 构建XML的简单示例: var pars = new Dictionary<string, string> { {"url","https://www. ...

  8. Linq对XML的简单操作

    前两章介绍了关于Linq创建.解析SOAP格式的XML,在实际运用中,可能会对xml进行一些其它的操作,比如基础的增删该查,而操作对象首先需要获取对象,针对于DOM操作来说,Linq确实方便了不少,如 ...

  9. ACEXML解析XML文件——简单示例程序

    掌握了ACMXML库解析XML文件的方法后,下面来实现一个比较完整的程序. 定义基本结构 xml文件格式如下 <?xml version="1.0"?> <roo ...

随机推荐

  1. C#线程运用基础

    ThreadStart ts=new ThreadStart(a.f);//ThreadStart 是一个委托,用以关联a.f方法Thread th=new Thread (ts);//Thread是 ...

  2. go语言的null值问题

    关于go语言数据库存储和显示null值的问题困扰了我很久,并且也和群友讨论过这个问题,但是都没有得到相对满意和全面的答案.最近FQ找了几篇相对详细和权威的文章,分享给大家,希望和大家一起进步,go g ...

  3. VS2017集成FastReport.Net并将模板保存到数据库

    本着开发与实施分离的思想,设计一个通用的报表设计窗体显得尤为重要(下图为图一): 要求与优点: I.报表设计窗体支持所有单据调用,一种单据支持多个打印模板. II.报表模板存储在数据库中.一是支持客户 ...

  4. C# autofac配置文件中设置单例

    设置instance-scope属性值为SingleInstance

  5. DataAnnotations 验证

    转自:http://blog.sina.com.cn/s/blog_c21a857b0102wcus.html 常用的 DataAnnotations 1.Required :属性值必须非空或者不能只 ...

  6. MahApps.Metro扁平化UI控件库(可修改主题色等)

    一.名词解释 使用MahApps.Metro扁平化UI控件库,可以使界面呈现更加美观.本文将总结MahApps.Metro的使用方法,及如何自定义修改其主题颜色等. 详细内容可参考官网:https:/ ...

  7. Android Source 源码已下载但 Android Studio 找不到的解决办法

    Android Studio 2.1 reporting in: solved the issue by resetting SDK. Preferences -> Appearance &am ...

  8. NGINX部署

  9. TOJ_12470

    #include <stdio.h> struct node{ int x; int y; int step;}first; int zx[4]={-1,0,1,0};int zy[4]= ...

  10. BZOJ 1008--[HNOI2008]越狱(容斥&快速幂)

    1008: [HNOI2008]越狱 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 12593  Solved: 5439[Submit][Status ...