Read XML (Recursive)

Source: Internet
Author: User


Document Description of XML
<?xml version= "1.0" encoding= "Utf-8" >

Each XML document must have, and can only have one node (XML case sensitive)

Tags generally exist in pairs, such as:<a></a>, but if the label has no sub data, it can be written as a closed <a/>

The storage structure of XML has the following two kinds
< tag name > value </label name >
< Tag Name property = Value Property 2 = value/>


For example:
<person name= "John" age= "gender=" "1"/>

========================================

XML uses <!--This is the annotation content--> to comment

<?xml version= "1.0" encoding= "Utf-8"?>
<studetn>
	<stuinfo id= "001" >
		<name> John </name>
		<age>25</age>
		<Gender>1</Gender>
	</stuinfo>
	< Stuinfo id= "002" >
		<name> Dick </name>
		<age>25</age>
		<gender>1</ gender>
	</stuinfo>
	<stuinfo id= "003" >
		<name> Harry </name>
		<age >25</age>
		<Gender>1</Gender>
	</stuinfo>
</studetn>

Another annotation of xml: <! [cdata[. ...]]. >

<news>
	<title> title </title>
	<content><a href= "http://www.baidu.com" > today's Headline News </a></content> 
	
	<!--problem comes, I just want to put a hyperlink in the content tag, but I this hyperlink is a <a></a> such a pair appears, XML will recognize it as a node, I don't like to make this a tag into a node, just think of him as content, how to do it. -->
</news>

Take a look at the following annotation method: <! [cdata[. ...]]. >

<?xml version= "1.0" encoding= "gb2312"?>
<news>
<title> title </title>
<content ><! [Cdata[<a href= "http://www.baidu.com" > today's Headline News </a>]]></content> 
</news>


To create an XML document with a C # statement

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Xml;
        Namespace XML {class Program {///<summary>///create an XML document///</summary> <param name= "args" ></param> static void Main (string[] args) {//namespace: Syst Em.
            XML;         Class Library: XmlDocument document//XmlElement element//XmlAttribute attribute//  XmlText content XmlDocument xdoc = new XmlDocument ();

            Represents an XML document. All elements use the document node to create XmlDeclaration Xdec = Xdoc.
            Createxmldeclaration ("1.0", "gb2312", null); Xdoc. AppendChild (XDEC);

            Adds the specified node to the end of the list of child nodes for the node. XmlElement Xele = Xdoc.
            createelement ("root"); Xdoc. AppendChild (Xele);

            Adds the specified node to the end of the list of child nodes for the node. XmlAttribute xattr = Xdoc.
            CreateAttribute ("id"); Xattr.value = "101"; Gets or setsThe value of the node. XmlText txt = xdoc.
            createTextNode ("I am a text node"); Xele. AppendChild (TXT);

            Adds the specified node to the end of the list of child nodes for the node. Xele. Attributes.append (XATTR);

            Inserts the specified attribute into the collection as the last node in the collection. Xdoc. Save ("D:/xml.")

        XML "); }
    }
}


Create a good XML document content:


Find the node: for example, under my e disk, there is a xml.xml file, and I want to get its parent node based on a value.

<?xml version= "1.0" encoding= "Utf-8"?>
<studetn>
	<stuinfo id= "001" >
		<name> John </name>
		<age>25</age>
		<Gender>1</Gender>
	</stuinfo>
	< Stuinfo id= "002" >
		<name> Dick </name>
		<age>25</age>
		<gender>1</ gender>
	</stuinfo>
	<stuinfo id= "003" >
		<name> Harry </name>
		<age >25</age>
		<Gender>1</Gender>
	</stuinfo>
</studetn>
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;

Using System.Xml.Linq; Namespace READXML {class Program {///<summary>///finds the parent node (gets all the contents of the parent node) based on the value of one content
           /</summary>///<param name= "args" ></param> static void Main (string[) args) { XDocument xdoc= xdocument.load ("E:/xml.xml")//from a file to load the XML document//the found content into the XElement, so we build a xelemen

           The generic collection of T, because the node we are looking for may have multiple list<xelement> List = new list<xelement> (); Searchelements (Xdoc.

        Root, list); public static void Searchelements (XElement ele, list<xelement> List) {//First traverse all child sections of Ele Point foreach (XElement item in Ele.
            Elements ())//elements () represents all the child nodes of the Ele node. That is, a collection of child elements. {//Determine if the name of this element is named, if the value stored in name is "John" if (item).
Name.localname = = "Name") {                    if (item. Value = = "John") {list. ADD (item. Parent)//Add the negative node of the "John" node to the list}///If there are child nodes in the item recursively (refer to recursion for reference ASP.N <a target=_blank href= "http://blog.csdn.net/fanbin168/article/details/45623263" target= "_blank" in et articles >
            TreeView Tree Menu node </a><span class= "Gray" ></span>) searchelements (item, list);
 }
        }
    }
}





 

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.