Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the document types of LINQ to XML

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "what are the document types of LINQ to XML", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "what are the document types of LINQ to XML"!

LINQ to XML can be thought of as a "better DOM" programming model that can interact with many members of a System.Xml.dll assembly.

I. Namespace

System.Xml.Linq.dll assemblies define three namespaces: System.Xml.Linq, System.Xml.Schema, and System.Xml.XPath

The core is System.Xml.Linq, which defines many types of aspects corresponding to XML documents.

Define XML document types

Second, create XML documents by programming

The previous .NET XML programming model required a lot of lengthy DOM API, while LINQ to XML could interact with XML documents in a DOM-independent manner. This not only greatly reduces the number of lines of code, but also the programming model can be directly mapped to a well-formed XML document structure.

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);}

Create an LINQ to XML document in memory

Static void CreateFunctionalXmlDoc ({XDocument inventoryDoc = new XDocument (new XDeclaration), 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");}

Third, create XML documents using LINQ queries

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);}

IV. Load and parse LINQ to XML content

Static void LoadExistingXml () {/ / Build an XElement from string. String myElement = @ "'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);}

5. Traversing LINQ to XML documents in memory

LINQ to XML example:

"0" > Ford Blue Chuck "1" > VW Silver Mary "2" > Yugo Pink Gipper "55" > Ford Yellow 862 CHAPTER 24 n PROGRAMMING WITH THE LINQ APIS Max "98" > BMW Black Zippy

LINQ to XML loading

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 traversal

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 query

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);}

VI. Modify LINQ to XML documents

Static void AddNewElements (XElement doc) {/ / Add 5 new purple Fords to the incoming document. For (int I = 0; I < 5; iTunes +) {/ / 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);} Thank you for your reading, these are the contents of "what are the document types of LINQ to XML?" after the study of this article, I believe you have a deeper understanding of what the document types of LINQ to XML have, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report