假如有以下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. iBatis多表查询

    <typeAlias alias="Product" type="com.shopping.entity.Product"/> <typeAl ...

  2. 中国人被“清朝GDP世界第一”忽悠了!

    中国人被"清朝GDP世界第一"忽悠了!"鸦片战争前的清朝GDP世界第一",这一说法在中国流传非常广.追根溯源,最早提出这一观点的似乎是英国学者麦迪森,他的一项猜 ...

  3. 开源服务发现项目Zookeeper,Doozer,Etcd

    这篇文章是Jason Wilder对于常见的服务项目发现Zookeeper.Doozer,Etcd所写的一篇博客,其原文地址例如以下:Open-Source Service Discovery. 服务 ...

  4. bnu 34986 Football on Table(数学+暴力)

    pid=34986" target="_blank" style="">题目连接:bnu 34986 Football on Table 题目大 ...

  5. C#操作Xml:XPath语法 在C#中使用XPath示例

    XPath可以快速定位到Xml中的节点或者属性.XPath语法很简单,但是强大够用,它也是使用xslt的基础知识. 示例Xml: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ...

  6. linux_java_redis_postgresql_常用命令

     redis 常用语法telnet 192.168.18.210 6379keys *llen队列名称llen 队列名称 postgresql常用语法psql -h192.168.18.210 -Up ...

  7. oracle_windows下命令启动oracle监听和服务

    1.检查监听器状态 C:\Users\Administrator>lsnrctl status 2.启动监听程序 C:\Users\Administrator>lsnrctl start ...

  8. java-新浪微博开放平台——话题跟踪

    代码 网盘地址:http://pan.baidu.com/s/1pJ1D0Kz

  9. 前端学习笔记(zepto或jquery)——对li标签的相关操作(一)

    对li标签的相关操作——点击li标签进行样式切换的两种方式 Demo演示: 1 2 3 4 // 详解: 第一种方式(以ul为基础): $("ul").bind("cli ...

  10. Go as continuous delivery tool for .NET

    http://simon-says-architecture.com/2014/02/28/go-as-continuous-delivery-tool-for-net/ Following my p ...