假如有以下xml文件

<?xml version="1.0" encoding="utf-8" ?>
<Date>
  <Products>
    <Product Name="West Size 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-bySondheim" SupplierID="2"/>
    <Supplier Name="Barbershop CDs" SupplierID="3"/>
  </Suppliers>
</Date>

  首先引用Xml.Linq命名空间,然后用以下方法进行读取

XDocument doc = XDocument.Load("XMLFile1.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")
                           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("SupplierName={0}---ProductName={1}",v.SupplierName,v.ProductName);
            }
            Console.ReadKey();

即可得到结果.

c# in deep 之LINQ读取xml(2)的更多相关文章

  1. Linq读取XML数据

    1.XML数据格式:<?xml version="1.0"?><customers>  <customer>    <id>ALFK ...

  2. Linq读取XML

    var customerList = ( from e in XDocument.Load("customers.xml"). Root.Elements("custom ...

  3. linq 读取xml

    xml 文件如下: <?xml version="1.0" encoding="utf-8" ?><nodes> <node> ...

  4. Linq to XML 读取XML 备忘笔记

    本文转载:http://www.cnblogs.com/infozero/archive/2010/07/13/1776383.html Linq to XML 读取XML 备忘笔记 最近一个项目中有 ...

  5. c# in deep 之LINQ简介(1)

    前两天公司进了一批书,在借阅jon skeet的c# in deep收获颇大,本书特点是介绍了不同版本的c#所增加的新特性.今天先写一下书中对linq的描述. 很多初学者在使用VS2010或2013写 ...

  6. .Net 读取xml

    一.常规方法 1.知识介绍 //初始化一个xml对象 XmlDocument xml = new XmlDocument(); //加载xml文件 xml.Load("文件路径") ...

  7. 应用Xml.Linq读xml文件

    c#提供了System.Xml.Linq操作xml文件,非常方便,本文主要介绍如何应用System.Xml.Linq读取xml文件. xml文本 <?xml version="1.0& ...

  8. 释放SQL Server占用的内存 .Net 读取xml UrlReWriter 在web.config中简单的配置

    释放SQL Server占用的内存   由于Sql Server对于系统内存的管理策略是有多少占多少,除非系统内存不够用了(大约到剩余内存为4M左右),Sql Server才会释放一点点内存.所以很多 ...

  9. C# LINQ学习笔记五:LINQ to XML

    本笔记摘抄自:https://www.cnblogs.com/yaozhenfa/p/CSharp_Linq_For_Xml.html,记录一下学习过程以备后续查用. 一.生成xml 1.1创建简单的 ...

随机推荐

  1. ASP.NET Web API中使用OData

    在ASP.NET Web API中使用OData 一.什么是ODataOData是一个开放的数据协议(Open Data Protocol)在ASP.NET Web API中,对于CRUD(creat ...

  2. rabbitMQ说明文档

    rabbitMQ是什么 RabbitMQ     是由     LShift     提供的一个     Advanced Message Queuing Protocol (AMQP)     的开 ...

  3. background-position 具体的使用说明

    语法: background-position : length || length background-position : position || position 值: length  : ...

  4. hive 的分隔符、orderby sort by distribute by的优化

    一.Hive 分号字符 分号是SQL语句结束标记,在HiveQL中也是,可是在HiveQL中,对分号的识别没有那么智慧,比如: select concat(cookie_id,concat(';',' ...

  5. kubernetes多节点部署的决心

    注:以下操作均基于centos7系统. 安装ansible ansilbe能够通过yum或者pip安装,因为kubernetes-ansible用到了密码.故而还须要安装sshpass: pip in ...

  6. crawler_jsoup HTML解析器_使用选择器语法来查找元素

    参照:http://www.open-open.com/jsoup/selector-syntax.htm 使用选择器语法来查找元素 问题 你想使用类似于CSS或jQuery的语法来查找和操作元素. ...

  7. 使用ArcGIS API for Silverlight 进行复合多条件空间查询

    原文:使用ArcGIS API for Silverlight 进行复合多条件空间查询 这两天帮网上认识的一个兄弟做了一个查询的示例,多多少少总结一下,在此和大家分享. 为什么说是复合多条件呢?因为进 ...

  8. Android开发之Mediaplayer

    Android提供了常见的音频.视频的编码.解码机制.借助于多媒体类MediaPlayer的支持,开发者能够非常方便在在应用中播放音频.视频.本篇博客主要解说在Android平台下怎样播放一个音频文件 ...

  9. ORACLE 中IN和EXISTS比较

    ORACLE 中IN和EXISTS比较 EXISTS的执行流程      select * from t1 where exists ( select null from t2 where y = x ...

  10. SQL点滴1—SET QUOTED_IDENTIFIER OFF语句的作用

    原文:SQL点滴1-SET QUOTED_IDENTIFIER OFF语句的作用 先看下面几个sql语句 代码   SELECT * FROM [USER]    WHERE a= 'netasp' ...