In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.