<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      LINQ to XML 編程基礎(chǔ)

       

      1、LINQ to XML類

       

      2010-05-05_151848

       

      以下的代碼演示了如何使用LINQ to XML來快速創(chuàng)建一個xml:

      隱藏行號 復(fù)制代碼 創(chuàng)建 XML
      1. public static void CreateDocument()
        
      2. {
        
      3.     string path = @"d:\website";
        
      4. 
        
      5.     XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
        
      6.                                    new XElement("Root", "root"));
        
      7. 
        
      8.     xdoc.Save(path);
        
      9. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8" standalone="yes"?>
      <Root>root</Root>

       

      2、XElement類

      XElement 類是 LINQ to XML 中的基礎(chǔ)類之一。 它表示一個 XML 元素。 可以使用該類創(chuàng)建元素;更改元素內(nèi)容;添加、更改或刪除子元素;向元素中添加屬性;或以文本格式序列化元素內(nèi)容。 還可以與 System.Xml 中的其他類(例如 XmlReader、XmlWriter 和 XslCompiledTransform)進(jìn)行互操作。

      使用LINQ to XML創(chuàng)建xml文檔有很多種方式,具體使用哪種方法要根據(jù)實際需要。而創(chuàng)建xml文檔最簡單、最常見的方式是使用XElement類。以下的代碼演示了如何使用XElement類創(chuàng)建一個xml文檔:

       

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void CreateCategories()
        
      2. {
        
      3.     string path = @"d:\website";
        
      4. 
        
      5.     XElement root = new XElement("Categories",
        
      6. 
        
      7.         new XElement("Category",
        
      8. 
        
      9.             new XElement("CategoryID", Guid.NewGuid()),
        
      10. 
        
      11.             new XElement("CategoryName", "Beverages")
        
      12.             ),
        
      13. 
        
      14.         new XElement("Category",
        
      15. 
        
      16.             new XElement("CategoryID", Guid.NewGuid()),
        
      17. 
        
      18.             new XElement("CategoryName", "Condiments")
        
      19. 
        
      20.             ),
        
      21. 
        
      22.         new XElement("Category",
        
      23. 
        
      24.             new XElement("CategoryID", Guid.NewGuid()),
        
      25. 
        
      26.             new XElement("CategoryName", "Confections")
        
      27. 
        
      28.             )
        
      29. 
        
      30.        );
        
      31. 
        
      32.     root.Save(path);
        
      33. 
        
      34. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category>
          <CategoryID>57485174-46fc-4e8c-8d98-d25b53d504a1</CategoryID>
          <CategoryName>Beverages</CategoryName>
        </Category>
        <Category>
          <CategoryID>1474dde1-8014-48f7-b093-b47ca5d5b770</CategoryID>
          <CategoryName>Condiments</CategoryName>
        </Category>
        <Category>
          <CategoryID>364224e0-e002-4939-90fc-0fd93e0cf35b</CategoryID>
          <CategoryName>Confections</CategoryName>
        </Category>
      </Categories>

       

      XElement類包含了許多方法,這些方法使得處理xml變得輕而易舉。有關(guān)這些方法請參照MSDN。

      其中,Save、CreateReader、ToString和WriteTo方法是比較常用的三個方法:

      2010-05-05_152909

       

      3、XAttribute類

       

      XAttribute類用來處理元素的屬性,屬性是與元素相關(guān)聯(lián)的“名稱-值”對,每個元素中不能有名稱重復(fù)的屬性。使用XAttribute類與使用XElement類的操作十分相似,下面的示例演示了如何在創(chuàng)建xml樹時為其添加一個屬性:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static XElement CreateCategoriesByXAttribute()
        
      2. {
        
      3.     XElement root = new XElement("Categories",
        
      4. 
        
      5.         new XElement("Category",
        
      6. 
        
      7.             new XAttribute("CategoryID", Guid.NewGuid()),
        
      8. 
        
      9.             new XElement("CategoryName", "Beverages")
        
      10. 
        
      11.             ),
        
      12. 
        
      13.         new XElement("Category",
        
      14. 
        
      15.             new XAttribute("CategoryID", Guid.NewGuid()),
        
      16. 
        
      17.             new XElement("CategoryName", "Condiments")
        
      18. 
        
      19.             ),
        
      20. 
        
      21.         new XElement("Category",
        
      22. 
        
      23.             new XAttribute("CategoryID", Guid.NewGuid()),
        
      24. 
        
      25.             new XElement("CategoryName", "Confections")
        
      26. 
        
      27.             )
        
      28.        );
        
      29. 
        
      30.     root.Save(path);
        
      31. 
        
      32.     return root;
        
      33. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category CategoryID="a6d5ef04-3f83-4e00-aeaf-52444add7570">
          <CategoryName>Beverages</CategoryName>
        </Category>
        <Category CategoryID="67a168d5-6b22-4d82-9bd4-67bec88c2ccb">
          <CategoryName>Condiments</CategoryName>
        </Category>
        <Category CategoryID="17398f4e-5ef1-48da-8a72-1c54371b8e76">
          <CategoryName>Confections</CategoryName>
        </Category>
      </Categories>

       

      XAttribute類的方法比較少,常用的三個是:

      2010-05-05_153201

       

      以下的示例使用Remove來刪除第一個元素的CategoryID屬性:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void RemoveAttribute()
        
      2. {
        
      3. 
        
      4.     XElement xdoc = CreateCategoriesByXAttribute();
        
      5. 
        
      6.     XAttribute xattr = xdoc.Element("Category").Attribute("CategoryID");
        
      7. 
        
      8.     xattr.Remove();
        
      9. 
        
      10.     xdoc.Save(path);
        
      11. 
        
      12. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category>
          <CategoryName>Beverages</CategoryName>
        </Category>
        <Category CategoryID="5c311c1e-ede5-41e5-93f7-5d8b1d7a0346">
          <CategoryName>Condiments</CategoryName>
        </Category>
        <Category CategoryID="bfde8db5-df84-4415-b297-cd04d8db9712">
          <CategoryName>Confections</CategoryName>
        </Category>
      </Categories>

       

      作為嘗試,試一試以下刪除屬性的方法:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void RemoveAttributeByDoc()
        
      2. {
        
      3. 
        
      4.     XElement xdoc = CreateCategoriesByXAttribute();
        
      5. 
        
      6.     XAttribute xattr = xdoc.Attribute("CategoryID");
        
      7. 
        
      8.     xattr.Remove();
        
      9. 
        
      10.     xdoc.Save(path);
        
      11. 
        
      12. }
        

      運行該示例將會拋出一個空引用異常,因為元素Categories沒有一個叫做CategoryID的屬性。

       

      4、XDocument類

       

      XDocument類提供了處理xml文檔的方法,包括聲明、注釋和處理指令。一個XDocument對象可以包含以下內(nèi)容:

      2010-05-05_161214

      下面的示例創(chuàng)建了一個簡單的xml文檔,它包含幾個元素和一個屬性,以及一個處理指令和一些注釋:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void CreateXDocument()
        
      2.       {
        
      3. 
        
      4.           XDocument xdoc = new XDocument(
        
      5. 
        
      6.                   new XProcessingInstruction("xml-stylesheet", "title='EmpInfo'"),
        
      7. 
        
      8.                   new XComment("some comments"),
        
      9. 
        
      10.                   new XElement("Root",
        
      11. 
        
      12.                           new XElement("Employees",
        
      13. 
        
      14.                                   new XElement("Employee",
        
      15. 
        
      16.                                           new XAttribute("id", "1"),
        
      17. 
        
      18.                                           new XElement("Name", "Scott Klein"),
        
      19. 
        
      20.                                           new XElement("Title", "Geek"),
        
      21. 
        
      22.                                           new XElement("HireDate", "02/05/2007"),
        
      23. 
        
      24.                                           new XElement("Gender", "M")
        
      25. 
        
      26.                                       )
        
      27. 
        
      28.                               )
        
      29. 
        
      30.                       ),
        
      31. 
        
      32.                   new XComment("more comments")
        
      33. 
        
      34.               );
        
      35. 
        
      36.           xdoc.Save(path);
        
      37. 
        
      38.       }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <?xml-stylesheet title='EmpInfo'?>
      <!--some comments-->
      <Root>
        <Employees>
          <Employee id="1">
            <Name>Scott Klein</Name>
            <Title>Geek</Title>
            <HireDate>02/05/2007</HireDate>
            <Gender>M</Gender>
          </Employee>
        </Employees>
      </Root>
      <!--more comments-->

       

      XDocument類包含多個與XElement類相同的方法,具體內(nèi)容可以參閱MSDN。需要注意的是,處理節(jié)點和元素的大部分功能都可以通過XElement獲得,只有當(dāng)絕對需要文檔層次的處理能力,以及需要訪問注釋、處理指令和聲明時,才有使用XDocument類的必要。

       

      創(chuàng)建了xml文檔后,可以使用NodesAfterSelf方法返回指定的XElement元素之后的所有同級元素。需要注意的是,此方法只包括返回集合中的同級元素,而不包括子代。此方法使用延遲執(zhí)行。以下代碼演示了這一過程:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void NodesAfterSelf()
        
      2. {
        
      3.     XElement root = new XElement("Categories",
        
      4.         new XElement("Category",
        
      5.                 new XElement("CategoryID", Guid.NewGuid()),
        
      6.                 new XElement("CategoryName", "食品"),
        
      7.                 new XElement("Description", "可以吃的東西")
        
      8.             )
        
      9.         );
        
      10.     foreach (var item in root.Element("Category").Element("CategoryID").NodesAfterSelf())
        
      11.     {
        
      12.         Console.WriteLine((item as XElement).Value);
        
      13.     }
        
      14. }
        

       

       

      二、LINQ to XML編程概念

       

      本節(jié)將介紹LINQ to XML編程的相關(guān)概念,例如如何加載xml、創(chuàng)建全新xml、操縱xml的信息以及遍歷xml文檔。

       

      1、加載已有的xml

      使用LINQ to XML加載xml可以從多種數(shù)據(jù)源獲得,例如字符串、XmlReader、TextReader或文件。

      下面的示例演示了如何從文件中加載xml:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. 
        
      2. public static void LoadFromFile()
        
      3. {
        
      4. 
        
      5.     XElement root = XElement.Load(path);
        
      6. 
        
      7.     Console.WriteLine(root.ToString());
        
      8. 
        
      9. }
        

      也可以使用Parse方法從一個字符串加載xml:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1.     public static void LoadFromString()
        
      2.     {
        
      3. 
        
      4.         XElement root = XElement.Parse(@"
        
      5. 
        
      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.         Console.WriteLine(root.ToString());
        
      23. 
        
      24.     }
        

       

      2、保存xml

      在前面的示例中曾多次調(diào)用XElement對象的Save方法來保存xml文檔,在這里就不冗述了。

       

      3、創(chuàng)建xml

      在前面的示例中曾多次調(diào)用XElement對象的構(gòu)造函數(shù)來創(chuàng)建xml文檔,在這里就不冗述了。需要說明的是,在使用LINQ to XML創(chuàng)建xml文檔時,會有代碼縮進(jìn),這使代碼的可讀性大大加強。

       

      4、遍歷xml

      使用LINQ to XML在xml樹中遍歷xml是相當(dāng)簡單的。只需要使用XElement和XAttribute類中所提供的方法。Elements和Element方法提供了定位到某個或某些元素的方式。下面的示例演示了如何遍歷xml樹,并獲取指定元素的方式:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      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. }
        

      上述代碼運行的結(jié)果為:

      clip_image002_thumb_1 

      是不是很簡單呢?Nodes()、Elements()、Element(name)和Elements(name)方法為xml樹的導(dǎo)航提供了基本功能。

       

      5、操縱xml

      LINQ to XML一個重要的特性是能夠方便地修改xml樹,如添加、刪除、更新和復(fù)制xml文檔的內(nèi)容。

      I.插入

      使用XNode類的插入方法可以方便地向xml樹添加內(nèi)容:

      2010-05-05_162103

      在下面的示例中,使用AddAfterSelf方法向現(xiàn)有xml中添加一個新節(jié)點:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      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. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category>
          <CategoryID>1</CategoryID>
          <CategoryName>Beverages</CategoryName>
          <AddDate>2010-01-31T03:08:51.813736+08:00</AddDate>
          <Description>Soft drinks, coffees, teas, beers, and ales</Description>
        </Category>
      </Categories>

      當(dāng)需要添加一個元素到指定節(jié)點之前時,可以使用AddBeforeSelf方法。

       

      II.更新

      在LINQ to XML中更新xml內(nèi)容可以使用以下幾種方法:

      2010-05-05_162242

      在下面的示例中使用了ReplaceWith與SetElementValue方法對xml進(jìn)行了更新操作:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      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. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category>
          <ID>2</ID>
          <CategoryName>test data</CategoryName>
          <Description>Soft drinks, coffees, teas, beers, and ales</Description>
        </Category>
      </Categories>

       

      III.刪除

      可以使用Remove(XElement)與RemoveAll方法來刪除xml。

      在下面的示例中,使用了RemoveAll方法:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. }
        
      2.   public static void Remove()
        
      3.   {
        
      4.       string path = @"d:\";
        
      5. 
        
      6.       XElement root = XElement.Parse(@"
        
      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.       root.RemoveAll();
        
      24. 
        
      25.       root.Save(path);
        
      26. 
        
      27.   }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories />

       

      在下面的示例中,使用了Remove方法刪除了xml的Description元素:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      1. public static void Remove()
        
      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("Description").Remove();
        
      15.     root.Save(path);
        
      16. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category>
          <CategoryID>1</CategoryID>
          <CategoryName>Beverages</CategoryName>
        </Category>
      </Categories>

       

      6、處理屬性

      I.添加

      LINQ to XML添加屬性與添加元素師類似的,可以使用構(gòu)造函數(shù)或者Add方法來添加屬性:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      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. }
        

      運行該示例將會得到一個xml文件,其內(nèi)容為:

      <?xml version="1.0" encoding="utf-8"?>
      <Categories>
        <Category CategoryID="1" AddDate="2010-01-31">
          <CategoryName>Beverages</CategoryName>
          <Description>Soft drinks, coffees, teas, beers, and ales</Description>
        </Category>
      </Categories>

       

      II.檢索

      檢索屬性可以使用Attribute(name)方法:

      顯示行號 復(fù)制代碼 這是一段程序代碼。
      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. }
        

      上述代碼的運行結(jié)果為:

      CategoryID

      1

       

      本文總結(jié)

       

      本文介紹了LINQ to XML的編程基礎(chǔ),即System.Xml.Linq命名空間中的多個LINQ to XML類,這些類都是LINQ to XML的支持類,它們使得處理xml比使用其他的xml工具容易得多。在本文中,著重介紹的是XElement、XAttribute和XDocument。 

       

      轉(zhuǎn)自: http://www.rzrgm.cn/sunnycoder/archive/2010/01/31/1660348.html

      posted @ 2010-05-05 15:23  luckdv  閱讀(67791)  評論(23)    收藏  舉報
      主站蜘蛛池模板: 免费ā片在线观看| 国产高清一区二区不卡| 亚洲午夜伦费影视在线观看| 成人拍拍拍无遮挡免费视频 | 亚洲第一极品精品无码久久| 乱人伦中文字幕成人网站在线| 欧美亚洲另类自拍偷在线拍| 国产精品美女一区二区三| 久久精品国产亚洲av品| 91产精品无码无套在线| 国产成人a在线观看视频免费| 国精品91人妻无码一区二区三区| 久久超碰色中文字幕超清| 久久不见久久见免费影院www日本| 天堂а√在线最新版中文在线| 亚洲精品毛片一区二区| 人人色在线视频播放| 国产肥臀视频一区二区三区| 亚洲国产良家在线观看| 国产成人精品a视频一区| 人妻系列无码专区无码中出| 2021AV在线无码最新| 精品视频不卡免费观看| 国产女人18毛片水真多1| 人人妻人人添人人爽日韩欧美| 中文字幕日本一区二区在线观看| 上虞市| 国产精品乱码久久久久久小说| 麻豆成人传媒一区二区| 精品久久人人做爽综合| 国产精品国产高清国产av| 欧美白人最猛性xxxxx| 国产一区日韩二区三区| 国产美女深夜福利在线一| 暖暖影院日本高清...免费| 国产欧美久久一区二区| 熟女系列丰满熟妇AV| 亚洲精品国产电影| 一区二区在线观看成人午夜| 东京热一精品无码av| 亚洲日本欧美日韩中文字幕|