Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use LINQ To XML

Shulou Source: shulou.com Published: 2022-06-02 08:58:40 10月02日 Update

This article mainly explains "how to use LINQ To XML". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to use LINQ To XML.

LINQ To XML--XML operation

XML data is more and more widely used in various practical development systems. In order to simplify the development and utilization of XML data, Microsoft's development team developed this brand-new LINQ to XML framework. First of all, the LINQ to XML framework is a lightweight XML programming API. Developers can almost replace the original XML data development method by using this framework, and it is very simple to create, read and manipulate XML data in memory, such as using function construction methods to create XML trees. Secondly, the powerful functions of LINQ are integrated into the LINQ to XML framework, and developers can adopt a consistent programming method to easily manipulate and query XML data.

XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", 20), new XElement ("Student", new XElement ("Name", "Li Si"), new XElement ("Sex", "female"), new XElement ("Age", 19)); Console.WriteLine (Students) Calendar XML XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", 20), new XElement ("Student", new XElement ("Name", "Li Si"), new XElement ("Sex", "female"), new XElement ("Age", 19) Foreach (XNode node in Students.Nodes ()) {Console.WriteLine (node); Console.WriteLine ("- -");} foreach (XElement ele in Students.Elements ()) {Console.WriteLine (ele); Console.WriteLine ("*");}

Add a XML nod

XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", 20), new XElement ("Student", new XElement ("Name", "Li Si"), new XElement ("Sex", "female"), new XElement ("Age", 19) Foreach (XElement ele in Students.Elements ()) {ele.Element ("Age") .AddAfterSelf (new XElement ("Hight", 173); ele.Element ("Age") .AddBeforeself (new XElement ("Weight", 73)); ele.Add (new XElement ("Hobby", "Coding")) } Console.WriteLine (Students) updates XML node XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", new XAttribute ("Year", 1989-8-22), 20)); Students.Element ("Student"). Element ("Age") .ReplaceWith (new XElement ("Age", 28)) / / replace the entire node / / Students.Element ("Student"). Element ("Age"). ReplaceNodes (28); / / replace only the node value / / Students.Element ("Student"). Element ("Age"). ReplaceAll (28); / replace the entire node Console.WriteLine (Students) Delete XML node XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", new XAttribute ("Year", "1989-8-22"), 20)); / / Students.Element ("Student") .Element ("Age"). Remove () / / remove the node / / Students.Element ("Student") .Element ("Age") .RemoveAll (); / / remove the node's value and attribute Students.Element ("Student") .Element ("Age") .RemoveNodes (); / / remove the node's value Console.WriteLine (Students) Add XML attributes XElement Students = new XElement ("Students", new XElement ("Student", new XElement ("Name", "Zhang San"), new XElement ("Sex", "male"), new XElement ("Age", new XAttribute ("Year", "1989-8-22"), 20)); Students.Element ("Student") .SetAttributeValue ("dd", "dddd"); Console.WriteLine (Students) Update XML attributes Students.Element ("Student"). Element ("Age") .ReplaceAttributes (new XAttribute ("Year", "dd"); Students.Element ("Student"). Element ("Age"). SetAttributeValue ("Year", "dddd"); delete XML attribute Students.Element ("Student"). Element ("Age"). Attribute ("Year"). Remove (); Students.Element ("Student"). Element ("Age"). RemoveAttributes () Traverse the XML attribute var Attr = from att in Students.Element ("Student"). Element ("Age"). Attributes () select att; foreach (var att in Attr) {Console.WriteLine (att);} Thank you for reading, the above is the content of "how to use LINQ To XML". After the study of this article, I believe you have a deeper understanding of how to use LINQ To XML, 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!

Tags: Node developer Zhang San attribute data Framework Li Si Learning content developer way Update programming powerful consistent New memory function function team Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux vpn Shulou Information macOS Docker