C # Read XML annotations

Source: Internet
Author: User

There is a need:

To count all the configuration files, these profiles are in XML format and write them to the data table, and if there are comments to write these comments to the corresponding Configuration object Description field

 <ItemID=""Key= "-1"FirstName=""Secondname=""Cssname= "hy"decription="">  </Item>  <!--111 - <ItemID=""Key= "-1"FirstName=""Secondname=""Cssname=""decription= "" ></item>

As shown above, the 111 note is written to the following line record object up

The final object is: obj={id= "", Firstname= "", Secondname= "", descrition= "111"}

This function simple can no longer simple, on-line products are very many, various configuration files are also very many, some have comments, some no comments, how to ensure that these comments are added to the corresponding configuration record up?

The key point is how to judge the relationship between annotations and configuration objects:

There are two types of root nodes that are read to an XML file: comments and ELEMENT nodes, the logic is: each time the node is read to determine the node type, if the type is a comment, just clear the last node type, save this node type, if it is

element node type, just determine whether the last node type is a comment type if it is just added to the Description field, and then empty the previous node type, record this time the node type,

The process is as follows:

Code:

Only the previous node element type is saved in Flaglist, 0 represents the annotation type, and 1 indicates the element type:
 varattributes =""; varKey =""; varTTTT = ( fromConfiginchXdoc.descendants ("Configuration"). Descendants ("appSettings")                        Selectconfig); foreach(varNodeinchtttt. Nodes ()) {if(node. NodeType = =System.Xml.XmlNodeType.Comment) {flaglist.                    Clear (); Attributes= node. ToString (). Replace ("<!--",""). Replace (" -",""); Flaglist. ADD (0); }                if(node. NodeType = =System.Xml.XmlNodeType.Element) {if(Flaglist. Count = =1)                    {                        if(flaglist[0] ==1) {Attributes=""; }} flaglist.                    Clear (); Key= ((XElement) node). Attribute ("Key").                    Value; Dic.                    ADD (key, attributes); Flaglist. ADD (1); }            }

C # Read XML annotations

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.