Linq To Xml

    CSDN广告是越来越多了,所有博客笔记不再更新,新网址 DotNet笔记

前言:Linq to xml与前面所说的XML API,不冲突,也不存在谁替代谁的说法,Linq To XML 只是简化了我们的对xml文档的操作.

添加引用,然后通过几个例子,说一下里面常用的类

using System.Xml.Linq;

1:XDocument 对象代表了xml文档

2:XElement 对象代表了元素

3:XAttribute 对象代表了属性


(一)

如下代码,创建一个通过使用XDocument,XElement,XAttribute对象创建一个xml文档

            XDocument xdoc = new XDocument(
                   new XElement("customers",
                       new XElement("customer",
                           new XAttribute("ID", "A"),
                           new XAttribute("City", "New York"),
                           new XAttribute("Region", "North America"),
                           new XElement("order",
                               new XAttribute("Item", "Widget"),
                               new XAttribute("Price", 100)
                           ),
                           new XElement("order",
                               new XAttribute("Item", "Tire"),
                               new XAttribute("Price", 200)
                           )
                       ),
                       new XElement("customer",
                           new XAttribute("ID", "B"),
                           new XAttribute("City", "Mumbai"),
                           new XAttribute("Region", "Asia"),
                           new XElement("order",
                                new XAttribute("Item", "Oven"),
                                new XAttribute("Price", 501)
                           )
                       )
                   )
               );
            xdoc.Save("D:\\xml.xml");

生成的xml文档:

文件:D:\xml.xml

<?xml version="1.0" encoding="utf-8"?>
<customers>
  <customer ID="A" City="New York" Region="North America">
    <order Item="Widget" Price="100" />
    <order Item="Tire" Price="200" />
  </customer>
  <customer ID="B" City="Mumbai" Region="Asia">
    <order Item="Oven" Price="501" />
  </customer>
</customers>

C#那一段代码,还有另一种写法,看一下模板,用"分开"的写法

文件:D:\xml2.xml

<?xml version="1.0" encoding="utf-8"?>
<customers>
  <customer ID="B" City="Mumbai" Region="Asia">
    <order Item="Oven" Price="501">MyOrder</order>
  </customer>
</customers>

代码如下:

            XDocument xdoc = new XDocument();
            XElement xe_customers = new XElement("customers");//根元素

            XElement xe_customer =new XElement("customer");
            XAttribute xa_id = new XAttribute("ID", "B");
            XAttribute xa_city = new XAttribute("City", "Mumbai");
            XAttribute xa_region = new XAttribute("Region", "Asia");
            xe_customer.Add(xa_id);
            xe_customer.Add(xa_city);
            xe_customer.Add(xa_region);


            XElement xe_order = new XElement("order","MyOrder");
            XAttribute xa_item = new XAttribute("Item", "Oven");
            XAttribute xa_price = new XAttribute("Price", 501);
            xe_order.Add(xa_item);
            xe_order.Add(xa_price);

            xe_customer.Add(xe_order);
            xe_customers.Add(xe_customer);
            xdoc.Add(xe_customers);
            xdoc.Save("D:\\xml2.xml");

上面这一段代码的方式,如果想从数据库中生成XML,配合上循环,也不错.

(二)

xml文档的读/查

1:实例:

 string xmlFileName = @"D:\xml2.xml";
            XDocument customers = XDocument.Load(xmlFileName);

            Console.WriteLine("Elements in loaded document:");
            //var queryResult = from c in customers.Elements()
            //                  select c.Name;
            //var queryResult = customers.Elements().Select(c=>c.Name);
            var queryResult = customers.Descendants();
            Console.WriteLine(queryResult.Count());
            foreach (var item in queryResult)
            {
                Console.WriteLine("name为:"+item.Name +"      "+"value为:"+item.Value+ "\r\n");
            }
            Console.Write("Press Enter/Return to continue:");
            Console.ReadLine();










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值