How to use Xml-based Linq to traverse stored data

Source: Internet
Author: User
Xml files are often used, but what is Xml? Xml files are used to store data. how can we traverse the data? Xml files are often used, but what is Xml?

Here is an example: testResult. xml file

 
   
      
   
    
Serial Number
     
    
      
   
    
Inspection items
     
    
      
   
    
Unit
     
    
      
   
    
Standard requirements
     
    
      
   
    
Inspection result
     
    
      
   
    
Conclusion
     
  
 

The above is an Xml file. we know that the Xml file is used to store data. how can we traverse the data?

In fact, the simplest method is to use Linq:

Private void GetTestResultXml () {List
 
  
ITestResultXml = new List
  
   
(); // Define and load the node (root node) XElement rootNode = XElement from the xml file. load (@".. \.. \ Xml \ testResult. xml "); // query statement: obtain the name subnode under the root node (at this time, the subnode can be cross-level: Sun node, Sun node ......) IEnumerable
   
    
TargetNodes = from target in rootNode. Descendants ("column") select target; foreach (XElement node in targetNodes) {iTestResultXml. Add (node. Value );}}
   
  
 

In this way, we can obtain All the data in the tag is stored in the list iTestResultXml.

In the testResult. xml file, we can see that, The tag sets its own id. this id is not his data, but his attribute,

So how can we obtain his attributes instead of the content in his tags?

Private void GetTestResultXml () {List
 
  
IXmlID = new List
  
   
(); // Define and load the node (root node) XElement rootNode = XElement from the xml file. load (@".. \.. \ Xml \ testResult. xml "); // query statement: obtain the name subnode under the root node (at this time, the subnode can be cross-level: Sun node, Sun node ......) IEnumerable
   
    
TargetNodes = from target in rootNode. descendants ("column") select target; foreach (XElement node in targetNodes) {iXmlID. add (node. attribute ("id "). value); // method for obtaining the specified attribute }}
   
  
 

In this way, we can get The list of id attributes in the tag iXmlID.

The above is the details about how to traverse stored data in Xml-based Linq. For more information, see other related articles in the first PHP community!

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.