zoukankan      html  css  js  c++  java
  • c# in deep 之LINQ读取xml(2)

        假如有以下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();

    即可得到结果.

    浮躁的人容易问:我到底该学什么;----别问,学就对了; 浮躁的人容易问:JS有钱途吗;----建议你去抢银行; 浮躁的人容易说:我要中文版!我英文不行!----不行?学呀! 浮躁的人分两种:只观望而不学的人;只学而不坚持的人; 浮躁的人永远不是一个高手。
  • 相关阅读:
    异常
    JAVA Math类
    Spring Cloud微服务Sentinel+Apollo限流、熔断实战总结
    JAVA之JDBC数据库连接池总结篇
    利用Python-docx 读写 Word 文档中的正文、表格、段落、字体等
    python3 最基本且简单的实现组合设计模式
    原生工程接入Flutter实现混编
    a[i][j] 和 a[j][i] 有什么区别?
    iconv函数报错 Detected an illegal character in input string
    用Python把20年的GDP、人口以及房价数据进行了可视化
  • 原文地址:https://www.cnblogs.com/xuekai-to-sharp/p/3336406.html
Copyright © 2011-2022 走看看