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 is LINQ to XML?

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

Share

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

This article introduces the knowledge of "what is LINQ to XML". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Before I introduce LINQ to XML to you in detail, let me first let you know about LINQ to Object.

LINQ to Object

MethodInfo [] methods = typeof (string). GetMethods (); var Q = from t in methods where (t.Name.Length > 15) select t

LINQ to Object is mainly implemented based on Lambda expressions in the .NET Framework, so the running result of the above code is equivalent to the following code:

MethodInfo [] methods = typeof (string). GetMethods (); var Q = methods .Where ((method) = > method.Name.Length > 15) .Select ((name) = > name.Name)

LINQ to XML

XElement xelem = XElement.Load (@ "example.xml"); / / query node name Item and return a collection of their PartNumber attribute values IEnumerable partNos = from item in xelem.Descendants ("Item") Select (string) item.Attribute ("PartNumber"); foreach (string strin partNos) Console.WriteLine (str)

In. Net 3.5, the framework extends the operation of XML, which is called LINQ to XML. Under the namespace System.Xml.LINQ. From the above code, we can see that a new XElement object has been added. We load the XML document through the XElement.Load method instead of the traditional DOM schema XmlDocument.Load.

Compared with the W3C DOM document structure, LINQ to XML provides us with a more convenient way to create an XML document.

XElement contacts = new XElement ("Contacts", new XElement ("Name", "Ice Lee"), new XElement ("Phone", "010-876546", new XAttribute ("Type", "Home")), new XElement ("Phone", "425-23456", new XAttribute ("Type", "Work"), new XElement ("Address", new XElement ("Street", "ZhiXinCun"), new XElement ("City", "Beijin") Output: Ice Lee 010-876546 425-23456 ZhiXinCun Beijing

LINQ to XML provides rich and concise classes to implement operations on XML. Compared with the XML class library of a wide variety of DOM models, LINQ classes make our learning curve smooth and achieve the same effect. LINQ to XML solves several inconvenient problems in the DOM model, such as changing node names, and abandons things that look powerful but rarely used, such as entities and entity references. This makes the operation of LINQ to XML faster and more convenient. The following examples will show you how LINQ to XML can modify, add and delete node names.

First, let's take a look at how adding a node to XML is implemented:

XElement xelem = XElement.Load (@ "example.xml"); XElement newnewXelem = new XElement ("NewNode", "This is new node"); xelem.Add (newXelem)

It's quite simple, as long as you turn it into a XElement object and Add it to the current node object. Let's take a closer look at how XElement can add nodes. You can see that there are five methods: Add, AddAfterSelf, AddAnnotation, AddBeforeSelf, and AddFirst. By default, the operation of Add inserts the new node as a child node of the inserted node, while AddFirst does the opposite. AddAfterSelf and AddBeforeSelf insert nodes as siblings. Note here that when calling these two methods, you cannot use the root node as the inserted node, because the XML document states that there can only be one root node. *, let's take a look at the AddAnnotation method.

AddAnnotation is a class object that adds a relevant comment to a node. This class object can be defined by the user, so through this method we can extend the function of the XML document object, such as getting the function of the class object according to the node. Here is a code quote from msdn:

Public class MyAnnotation {private string tag; public string Tag {get {return tag;} set {tag = value;}} public MyAnnotation (string tag) {this.tag = tag;} … ... MyAnnotation ma = new MyAnnotation ("T1"); XElement root = new XElement ("Root", "content"); root.AddAnnotation (ma); MyAnnotation ma2 = (MyAnnotation) root.Annotation (); that's all for "what is LINQ to XML". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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