LINQ TO XML

LINQ to XML 提供了改进的 XML 编程接口,这一点可能与 LINQ to XML 的 LINQ 功能同样重要。 通过 LINQ to XML,对 XML 编程时,您可以实现任何预期的操作,包括:

  • 从文件或流加载 XML。

  • 将 XML 序列化为文件或流。

  • 使用函数构造从头开始创建 XML。

  • 使用类似 XPath 的轴查询 XML。

  • 使用 Add、Remove、ReplaceWith 和 SetValue 等方法对内存 XML 树进行操作。

  • 使用 XSD 验证 XML 树。

  • 使用这些功能的组合,可将 XML 树从一种形状转换为另一种形状。



1、读取XML文档


   从文件中加载:

XElement root = XElement.Load(path);
 
从内存中加载:
 
 
  1. XElement root = XElement.Parse(@"
    
  2. 
    
  3.     <Categories>
    
  4. 
    
  5.       <Category>
    
  6. 
    
  7.         <CategoryID>1</CategoryID>
    
  8. 
    
  9.         <CategoryName>Beverages</CategoryName>
    
  10. 
    
  11.         <Description>Soft drinks, coffees, teas, beers, and ales</Description>
    
  12. 
    
  13.       </Category>
    
  14. 
    
  15.     </Categories>
    
  16. 
    
  17. ");
    




2、基本操作:添加、删除、修改、保存节点操作


 添加:


  1. public static void AddAfterSelf()
    
  2.  
  3. {
    
  4.  
  5.     XElement root = XElement.Parse(@"
    
  6. 
    
  7.         <Categories>
    
  8. 
    
  9.           <Category>
    
  10. 
    
  11.             <CategoryID>1</CategoryID>
    
  12. 
    
  13.             <CategoryName>Beverages</CategoryName>
    
  14. 
    
  15.             <Description>Soft drinks, coffees, teas, beers, and ales</Description>
    
  16. 
    
  17.           </Category>
    
  18. 
    
  19.         </Categories>
    
  20. 
    
  21.     ");
    
  22.  
  23.     XElement xele = root.Element("Category").Element("CategoryName");
    
  24.  
  25.     xele.AddAfterSelf(new XElement("AddDate", DateTime.Now));
    
  26.  
  27.     root.Save(path);
    
  28.  
  29. }


修改:

  1. public static void Update()
    
  2. {
    
  3.  
  4.     XElement root = XElement.Parse(@"
    
  5.                                    <Categories>
    
  6.                                       <Category>
    
  7.                                         <CategoryID>1</CategoryID>
    
  8.                                         <CategoryName>Beverages</CategoryName>
    
  9.                                         <Description>Soft drinks, coffees, teas, beers, and ales</Description>
    
  10.                                       </Category>
    
  11.                                     </Categories>
    
  12.                                   ");
    
  13.  
  14.     root.Element("Category").Element("CategoryID").ReplaceWith(new XElement("ID", "2"));
    
  15.     root.Element("Category").SetElementValue("CategoryName", "test data");
    
  16.     root.Save(path);
    
  17. }

删除:

  1.  public static void Remove()
    
  2.   {
    
  3.       string path = @"d:\";
    
  4.  
  5.       XElement root = XElement.Parse(@"
    
  6.                                   <Categories>
    
  7. 
    
  8.                                     <Category>
    
  9. 
    
  10.                                       <CategoryID>1</CategoryID>
    
  11. 
    
  12.                                       <CategoryName>Beverages</CategoryName>
    
  13. 
    
  14.                                       <Description>Soft drinks, coffees, teas, beers, and ales</Description>
    
  15. 
    
  16.                                     </Category>
    
  17. 
    
  18.                                   </Categories>
    
  19. 
    
  20.                                 ");
    
  21.  
  22.       root.RemoveAll();
    
  23.  
  24.       root.Save(path);
    
  25.  
  26.   }

添加属性

  1. public static void AddAttribute()
    
  2. {
    
  3.     XElement root = new XElement("Categories",
    
  4.         new XElement("Category",
    
  5.             new XAttribute("CategoryID", "1"),
    
  6.             new XElement("CategoryName", "Beverages"),
    
  7.             new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
    
  8.         )
    
  9.     );
    
  10.  
  11.     root.Element("Category").Add(new XAttribute("AddDate", DateTime.Now.ToShortDateString()));
    
  12.     root.Save(path);
    
  13. }


3、查询语法


检索属性:

  1. public static void SelectAttribute()
    
  2. {
    
  3.     XElement root = new XElement("Categories",
    
  4.         new XElement("Category",
    
  5.             new XAttribute("CategoryID", "1"),
    
  6.             new XElement("CategoryName", "Beverages"),
    
  7.             new XElement("Description", "Soft drinks, coffees, teas, beers, and ales")
    
  8.         )
    
  9.     );
    
  10.  
  11.     XAttribute xattr = root.Element("Category").Attribute("CategoryID");
    
  12.     Console.WriteLine(xattr.Name);
    
  13.     Console.WriteLine(xattr.Value);
    
  14. }

遍历:

  1. public static void Enum()
    
  2.  
  3. {
    
  4.  
  5.     XElement root = new XElement("Categories");
    
  6.  
  7.     using (NorthwindDataContext db = new NorthwindDataContext())
    
  8.  
  9.     {
    
  10.  
  11.         root.Add(
    
  12.  
  13.                 db.Categories
    
  14.  
  15.                 .Select
    
  16.  
  17.                 (
    
  18.  
  19.                     c => new XElement
    
  20. 
    
  21.                     (
    
  22.  
  23.                         "Category"
    
  24. 
    
  25.                         , new XElement("CategoryName", c.CategoryName)
    
  26.  
  27.                     )
    
  28.  
  29.                 )
    
  30.  
  31.             );
    
  32.  
  33.     }
    
  34.  
  35.     foreach (var item in root.Elements("Category"))
    
  36.     {
    
  37.         Console.WriteLine(item.Element("CategoryName").Value);
    
  38.  
  39.     }
    
  40.  
  41. }

4、加载复杂对象

          var rooms = from room in doc.Descendants("Room")
                         select new
                         {
                             RoomID = room.Element("RoomID").Value,
                             Prices = from price in XDocument.Parse(room.ToString()).Descendants("Price")
                                      select new
                                      {
                                          PriceCode = price.Element("PriceCode").Value,
                                          Stays = from stay in XDocument.Parse(price.ToString()).Descendants("Stay")
                                                  select new
                                                  {
                                                      Date = stay.Element("Date").Value,
                                                      Rate = stay.Element("Rate").Value,
                                                      RoomStatus = stay.Element("RoomStatus").Value,
                                                      BreakfastType = stay.Element("BreakfastType").Value
                                                  }
                                      }
                         };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值