24.c#linq to XML (12 Chapter 12.3)

Source: Internet
Author: User



Oneself also wrote so many, but still have a lot of don't understand, a bit impetuous bar, but Rice still want to eat Ah, say say LINQ to XML bar.



LINQ to Xml is located in the System.Xml.Linq assembly, and most types are located in the System.Xml.Linq namespace. Almost all types under this namespace are prefixed with x, and the element in the common DOM API corresponds to the XElement in LINQ to XML. List what types are listed below.


    1. XName: Represents the names of elements and attributes
    2. XNamespace: A namespace representing XML, usually a URL
    3. XObject: Is the common parent of XNode and XAttribute: Unlike DOM APIs, attributes are not nodes in LINQ to XML. If a method returns the element of a child node, this contains no attributes.
    4. XNode: Represents a node in an XML tree that defines a variety of members for operations and query trees.
    5. XAttribute: Represents an attribute that contains a name/value pair, and the value is essentially text, but can be explicitly converted to another data type
    6. XContainer: is a node in the XML tree that contains child content
    7. XText: Represents a text node whose derived class, Xcdata, is a CDATA text node
    8. XElement: It and XAttribute are the most commonly used classes in LINQ to XML.
    9. XDocument: Represents a document
    • The Add method that inherits from XContainer has the following points:
    1. Null references are ignored
    2. XNode and XAttribute instances can be added
    3. string, number, date, time, etc. converted to Xtext using standard XML format
    4. Other objects that do not have special handling will call ToString () to convert it to text
  • Several instances of using
    
    
    
    
     
    
     
    
    1 var element = new XElement("root", new XElement("child"));
    2 Console.WriteLine(element);
     
    
    1 <root>
    2   <child />
    3 </root>
     
    
    1 var element1 = new XElement("root", new XElement("child", DateTime.Now));
    2 Console.WriteLine(element1);
     
    
    1 <root>
    2   <child>2015-01-28T22:35:41.9713268+08:00</child>
    3 </root>
     
    
    1 var list = new List<User> {
    2 new User {Name="a",Age=1 },
    3 new User {Name="b",Age=2 },
    4 new User {Name="c",Age=3 },
    5 new User {Name="d",Age=4 }
    6 };
    7 
    8 var element2 = new XElement("root", list.Select(user => new XElement("child", user.Name)));
    9 Console.WriteLine(element2);
     
    
    1 var element3 = new XElement("root", list.Select(user => new XElement("child", new XAttribute("name", user.Name), new XAttribute("age", user.Age))));
    2 Console.WriteLine(element3);
     
    
     1 <root>
     2   <child>a</child>
     3   <child>b</child>
     4   <child>c</child>
     5   <child>d</child>
     6 </root>
     7 
     8 
     9 <root>
    10   <child name="a" age="1" />
    11   <child name="b" age="2" />
    12   <child name="c" age="3" />
    13   <child name="d" age="4" />
    14 </root>




  • Querying a single node


There are too many iterations for XElement, XElement contains a number of axes methods (axis method, personal understanding is the direct method), such as what points, such as the new Element ("root"). Elements is the New Element ("root") that returns all the child nodes under root. Attributes is all the feature nodes in Root. Let's just list it, but that's more than that.


    1. Ancestors: Ancestor node
    2. Descendantnodes: Descendant nodes
    3. Annotations: note (note is also a node)
    4. Elements: Child nodes
    5. Descendants: descendants
    6. Node....


The sequence of nodes returned by the axis method used by a single node can be fully queried using LINQ, or by using extension methods, you can see that LINQ is using the same method (for our users), from querying the object in memory to the data in the database, to the XML, in a perfect combination.



Please treatise.



24.c#linq to XML (Chapter 12 12.3)


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.