码迷,mamicode.com
首页 > 其他好文 > 详细

XML+LINQ

时间:2016-07-22 12:52:52      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:

data.xml

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<Data>
  <Products>
    <Product Name="West Side Story" Price="9.99m" SuppelierID="1"/>
    <Product Name="Assassins" Price="14.99m" SuppelierID="2"/>
    <Product Name="Frogs" Price="13.99m" SuppelierID="1"/>
    <Product Name="Sweeney Todd" Price="9.99m" SuppelierID="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>
View Code

应用

技术分享
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class XML_LINQ
    {
        static void Main()
        {
            //将产品和供货商连接起来,价格高于10美元,先按供货商名排序,在按产品名排序,最后打印供货商名和产品名
            //供货商和产品编码使用XML文件
            XDocument doc = XDocument.Load("data.xml");    //XDocument使用命名空间System.Xml.Linq;
            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") > 10
                            orderby (string)s.Attribute("Name"), (string)p.Attribute("Name")
                            select new
                            {
                                SupplierName = (string)s.Attribute("Name"),
                                ProductName = (string)s.Attribute("Name")
                            };
            foreach (var v in filtered)
            {
                Console.WriteLine("Supplier={0};Product={1}", v.SupplierName, v.ProductName);
            }
        }
    }
}
View Code

 

XML+LINQ

标签:

原文地址:http://www.cnblogs.com/Sukie-s-home/p/5694584.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!