24. C # linq to xml (chapter 12, 12.3 ),

Source: Internet
Author: User

24. C # linq to xml (chapter 12, 12.3 ),

I have written so much about it myself, but I still have a lot TO understand. It's a little impetuous, but I still want TO eat it. Let's talk about LINQ TO XML.

Linq to xml is located in the System. Xml. Linq assembly, and most types are located in the System. Xml. Linq namespace. In this namespace, almost all types are prefixed with X. elements in common DOM APIs correspond TO xelements in linq to xml. Lists all types.

  • The Add method inherited from XContainer has the following points:
  • Several instances used
    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>

     

  • Query A Single Node

For XElement, there are too many items that can be iterated. XElement contains many axis methods (the axis method is a straightforward method in my personal understanding), such as what points, for example, New Element ("root "). elements is to return all the child nodes under the root, New Element ("root "). attributes is all feature nodes in the root. Let's just list it.

For the node sequence returned by the axis method used by a single node, you can use LINQ to query the node sequence. Alternatively, you can use the extension method to perform some operations. You can see that the data from the queried object in the memory to the database is returned by LINQ, in XML, we use the same method (for our users) to perfectly combine them.

Please make an axe.

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.