Notes for learning from LINQ to XML

Source: Internet
Author: User

I have learned a little about XML file parsing before, and I have read a variety of parsing methods on and off. We all say that it is the easiest to count the data in the format of LINQ to XML. However, it is always unsuccessful to try it on your own several times, and the search tutorials are too complicated and obscure. This internship in the Organization has the need to parse XML, only to try it, and the result is still successful! To sum up, you still need to have a clear idea. xml files are some nodes and attributes. As long as you have mastered the corresponding hierarchy, the Code logic is clear and the syntax is correct!

It is really easy to write the following code:

 

The XML file is as follows:

<? XML version = "1.0" encoding = "UTF-8"?> <Items> <item name = "disconnection warning caused by access/switch conflicts"> <type name = "RX power"> <min value = "" operator = ""> </ min> <max value = "" operator = ""> </max> </type> <type name = "TX power"> <min value = "" operator = ""> </min> <max value = "" operator = ""> </max> </type> <type name = "EC/IO"> <min value = "" operator = ""> </min> <max value = "" operator = ""> </max> </type> </item> <item name = "caused by Overwrite out-of-the-box warning "> <type name =" RX power "> <min value =" "operator =" "> </min> <max value ="-100 "operator =" & lt; = "> </max> </type> <type name =" TX power "> <min value =" 23 "operator =" & gt; = "> </min> <max value =" "operator =" "> </max> </type> <type name =" EC/IO "> <min value = "" operator = ""> </min> <max value = "-15" operator = "& lt; "> </max> </type> <type name =" TX adj "> <min value =" 10 "operator =" & gt; "> </min> <max value =" 0 "operator =" & lt; "> </max> </type> </item> </items>

 

Note: Because the <and> symbol is already in use in the XML file, you can use the <symbol and> symbol if you want to use the symbol.

 

XML parsing code

Public static xml_value_class read (string path) {xelement root = xelement. load (PATH); // read the file xml_value_class xmlvalue = new xml_value_class (); // XML class xattribute itemname = NULL; xattribute typevaluename = NULL; xattribute typevaluemin = NULL; xattribute typevaluemax = NULL; xattribute typevalueminoperator = NULL; xattribute typevaluemaxoperator = NULL; foreach (VAR item in root. elements ("item") // traverses the node item {itemname = item. attribute ("name"); // The name attribute foreach (VAR type in item. elements ("type") traverses the node type {typevaluename = type. attribute ("name"); // The property typevaluemin = type of the node type. element ("min "). attribute ("value"); // here is the min attribute of the node. Because the node has been traversed to the type node, the corresponding node can be directly read from VAR type, then retrieve the property typevaluemax = type. element ("Max "). attribute ("value"); typevalueminoperator = type. element ("min "). attribute ("operator"); typevaluemaxoperator = type. element ("Max "). attribute ("operator"); xmlvalue. value (itemname. value, typevaluename. value, typevaluemax. value, typevaluemin. value, typevaluemaxoperator. value, typevalueminoperator. value); // here is the function that obtains the corresponding value and assigns it to the corresponding object. For example, the itemname value statement is itemname. value; here, itemname is only an attribute. The value displayed during the debugging process is item name = "Access/switch conflicts that cause a session disconnection warning". Therefore, you need to use it. value operation to retrieve the property stored value} return xmlvalue ;}

 

There are also file write operations. Since the file has been read, you can directly use = to assign values to the attribute, or use itemname. setvalue (value); to modify the attribute value. Last root. Save (PATH );

 

I also learned some data operations this time:

String. Empty; // indicates an empty string, that is ""

Convert. todouble ("12"); // convert a string to a double type. The string content must be a numeric character.

Double = double. Nan; // It indicates that the secondary field is a constant instead of a number. This is used to assign values to the double type when the max and Min attributes are string. Empty.

Double. isnan (double) // returns the bool value to determine whether the double type data is a number.

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.