溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

LINQ to XML的文檔類型有哪些

發(fā)布時(shí)間:2021-12-01 16:09:10 來(lái)源:億速云 閱讀:139 作者:iii 欄目:編程語(yǔ)言

這篇文章主要講解了“LINQ to XML的文檔類型有哪些”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“LINQ to XML的文檔類型有哪些”吧!

LINQ to XML可以看作是一個(gè) “better DOM” 編程模型,可以和 System.Xml.dll 程序集中的很多成員交互。

一、命名空間

System.Xml.Linq.dll 程序集定義了三個(gè)命名空間:System.Xml.Linq, System.Xml.Schema  和 System.Xml.XPath

最核心的是 System.Xml.Linq, 定義了對(duì)應(yīng) XML 文檔個(gè)方面的很多類型

LINQ to XML的文檔類型有哪些 
定義XML文檔類型

二、編程方式創(chuàng)建XML文檔

以前的 .NET XML編程模型需要使用很多冗長(zhǎng)的 DOM API,而 LINQ to XML 則完全可以用與 DOM 無(wú)關(guān)的方式與 XML 文檔交互。這樣不但大大減少了代碼行,而且這種編程模型可以直接映射到格式良好的XML文檔結(jié)構(gòu)。

static void CreateFunctionalXmlElement()  {  // A "functional" approach to build an  // XML element in memory.  XElement inventory =  new XElement("Inventory",  new XElement("Car", new XAttribute("ID", "1"),  new XElement("Color", "Green"),  new XElement("Make", "BMW"),  new XElement("PetName", "Stan")  )  );  // Call ToString() on our XElement.  Console.WriteLine(inventory);  }

在內(nèi)存中創(chuàng)建LINQ to XML文檔

static void CreateFunctionalXmlDoc(         {             XDocument inventoryDoc =             new XDocument(             new XDeclaration("1.0", "utf-8", "yes"),             new XComment("Current Inventory of AutoLot"),             new XElement("Inventory",             new XElement("Car", new XAttribute("ID", "1"),             new XElement("Color", "Green"),             new XElement("Make", "BMW"),             new XElement("PetName", "Stan")             ),             new XElement("Car", new XAttribute("ID", "2"),             new XElement("Color", "Pink"),             new XElement("Make", "Yugo"),             new XElement("PetName", "Melvin")             )             )             );             // Display the document and save to disk.             Console.WriteLine(inventoryDoc);             inventoryDoc.Save("SimpleInventory.xml");         }

三、使用LINQ查詢創(chuàng)建XML文檔

static void CreateXmlDocFromArray()  {  // Create an anonymous array of anonymous types.  var data = new [] {  new { PetName = "Melvin", ID = 10 },  new { PetName = "Pat", ID = 11 },  new { PetName = "Danny", ID = 12 },  new { PetName = "Clunker", ID = 13 }  };  // Now enumerate over the array to build  // an XElement.  XElement vehicles =  new XElement("Inventory",  from c in data  select new XElement("Car",  new XAttribute("ID", c.ID),  new XElement("PetName", c.PetName)  )  );  Console.WriteLine(vehicles);  }

四、加載和解析LINQ to XML內(nèi)容

static void LoadExistingXml()          {              // Build an XElement from string.              string myElement =                                          @"<CAR ID =< SPAN>'3'>                              Yellow                             Yugo                             ";              XElement newElement = XElement.Parse(myElement);              Console.WriteLine(newElement);              Console.WriteLine();              // Load the SimpleInventory.xml file.              XDocument myDoc = XDocument.Load("SimpleInventory.xml");              Console.WriteLine(myDoc);          }

五、遍歷內(nèi)存中的LINQ to XML 文檔

LINQ to XML 示例:

<?xml version="1.0" encoding="utf-8"?>     <CAR CARID =< SPAN>"0">      Ford     Blue     Chuck      <CAR CARID =< SPAN>"1">      VW     Silver     Mary      <CAR CARID =< SPAN>"2">      Yugo     Pink     Gipper      <CAR CARID =< SPAN>"55">      Ford     Yellow     862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS      Max      <CAR CARID =< SPAN>"98">      BMW     Black     Zippy

LINQ to XML 加載

static void Main(string[] args)          {              Console.WriteLine("***** Fun with LINQ to XML *****\n");              // Load the Inventory.xml document into memory.              XElement doc = XElement.Load("Inventory.xml");              // We will author each of these next              PrintAllPetNames(doc);              Console.WriteLine();              GetAllFords(doc);              Console.ReadLine();          }

LINQ to XML遍歷

static void PrintAllPetNames(XElement doc)  {  var petNames = from pn in doc.Descendants("PetName")  select pn.Value;  foreach (var name in petNames)  Console.WriteLine("Name: {0}", name);  }

LINQ to XML查詢

static void GetAllFords(XElement doc)          {              var fords = from c in doc.Descendants("Make")                          where c.Value == "Ford"                         select c;              foreach (var f in fords)                  Console.WriteLine("Name: {0}", f);          }

六、修改LINQ to XML 文檔

static void AddNewElements(XElement doc)  {  // Add 5 new purple Fords to the incoming document.  for (int i = 0; i < 5; i++)  {  // Create a new XElement  XElement newCar =  new XElement("Car", new XAttribute("ID", i + 1000),  new XElement("Color", "Green"),  new XElement("Make", "Ford"),  new XElement("PetName", "")  );  // Add to doc.  doc.Add(newCar);  }  // Show the updates.  Console.WriteLine(doc);  }

感謝各位的閱讀,以上就是“LINQ to XML的文檔類型有哪些”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)LINQ to XML的文檔類型有哪些這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI