假如有以下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. 产品 线上 保持 和 支持 服务 (Support and maintenance solutions)

    Maintenance and support are the key factors for the smooth functioning of ERP solutions. ERP mainten ...

  2. MySQL进口.sql文件和常用命令

    MySQL进口.sql文件和常用命令 在MySQL Qurey   Brower中直接导入*.sql脚本,是不能一次运行多条sql命令的.在mysql中运行sql文件的命令: mysql> so ...

  3. 第19章 解释器模式(Interpreter Pattern)

    原文 第19章 解释器模式(Interpreter Pattern) 解释器模式 导读:解释器模式,平常用的比较的少,所以在写这个模式之前在博客园搜索了一番,看完之后那叫一个头大.篇幅很长,我鼓足了劲 ...

  4. 【spring教程之二】spring注射剂xml构造方法参数

    1.上述续,假设你想注入bean当文件,传递给构造函数的参数.主要的变化是需要spring.xml配置文件来配置. <?xml version="1.0" encoding= ...

  5. Cocos2d-x 3.2 大富翁游戏项目开发-第五部分 单机游戏-级别选择ScrollView

    于MenuScene.cpp 点击单机游戏后会调用 Director::getInstance()->pushScene(MapChooseScene::createScene()); 进入到关 ...

  6. python_基础学习_01_按行读取文件的最优方法

    python 按行读取文件 ,网上搜集有N种方法,效率有区别,先mark最优答案,下次补充测试数据 with open('filename') as file: for line in file: d ...

  7. Cocos发育Visual Studio下一个HttpClient开发环境设置

    Cocos2d-x 3.x相关类集成到网络通信libNetwork图书馆project于.这其中包括:HttpClient分类. 我们需要在Visual Studio溶液中加入libNetwork图书 ...

  8. ShellExecuteEx的使用方法

    关于怎样在c++中启动外部的exe程序,之前看到在百度一搜就看到了: ShellExecute(this->m_hWnd,"open","calc.exe" ...

  9. CSS3火焰文字特效制作教程

    原文:CSS3火焰文字特效制作教程 用一句很俗气的话概括这两天的情况就是:“最近很忙”,虽然手头上有不少很酷的HTML5和CSS3资源,但确实没时间将它们的实现过程写成教程分享给大家.今天刚完成了一个 ...

  10. CSDN博客频道维护公告

    各位亲爱的用户:        为了给大家提供更稳定的使用环境,2014年4月23日23点至04月24日1点(本周四凌晨)博客频道server将进行维护,维护期间不能正常訪问.给大家带来不便,敬请广大 ...