[Original] C # xml operations

Source: Internet
Author: User
1. load XML
A. Load existing XML 1 Xmldocument Doc = New Xmldocument ();
Doc. Load (server. mappath ( " Students. xml " ));

B. Create XML Xmldocument Doc = New Xmldocument ();
String Strxml = " <Students>
<Student> <ID> 1 </ID> < Name > Hyq </ Name > < Age > 24 </ Age > </ Student >
< Student > < ID > 2 </ ID > < Name > Hyq2 </ Name > < Age > 25 </ Age > </ Student >
< Student > < ID > 3 </ ID > < Name > Hyq3 </ Name > < Age > 26 </ Age > </ Student >
< Student > < ID > 4 </ ID > < Name > Hyq4 </ Name > < Age > 27 </ Age > </ Student >
</ Students > " ;
Doc. loadxml (strxml );

2. Obtain the values of all lower-level nodes. 1 Xmldocument Doc=NewXmldocument ();
Doc. Load (server. mappath ("Students. xml"));

2 Xmlnodelist topm =  Doc. documentelement. Childnodes;
3 Stringbuilder Str =   New Stringbuilder ( " <Ul> " );
4 Foreach (Xmlnode Node In Topm)
5 {
6
7 Str. append ( " <Li> Name: "   + Node. childnodes [ 1 ]. Innertext +   " ; Age: "   + Node. childnodes [ 2 ]. Firstchild. innertext +   " </LI> " );
8
9 }
10 Str. append ( " </Ul> " );
11 Lb_xml.text = Str. tostring ();

PS: above Code The "green" part of the node is used to obtain the root node. Another method is to obtain all nodes. If you only obtain some nodes, we recommend that you use this method. Selectnodes
1 Xmlnodelist nodelists = Doc. selectnodes ( " Students/student " )
2   Foreach (Xmlnode Node In Nodelists)
3 {
4
5 Str. append ( " <Li> Name: "   + Node. childnodes [ 1 ]. Innertext +   " ; Age: "   + Node. childnodes [ 2 ]. Firstchild. innertext +   " </LI> " );
6
7 }
8 Str. append ( " </Ul> " );
9 Lb_xml.text = Str. tostring ();

3. Add nodes Add an existing node copy
1 Xmlnode node2 = Doc. documentelement. childnodes [ 0 ]. Clonenode ( True );
2 Node2.childnodes [ 0 ]. Innertext =   " 6 " ;
3 Node2.childnodes [ 1 ]. Innertext =   " Second " ;
4 Node2.childnodes [ 2 ]. Innertext =   " 135 " ;
5 Doc. documentelement. appendchild (node2 ); Add new nodes
1 Xmlnode node1 = Doc. createnode (xmlnodetype. element, " Job " , "" );
2 Xmlnode node11 = Doc. createnode (xmlnodetype. element, " Name " , "" );
3 Node11.innertext =   " Net " ;
4 Node1.appendchild (node11 );
5 Doc. documentelement. appendchild (node1 );

PS: In this example, adding a node under the root node is used as an example. If the node is changed accordingly, the node or attribute can be added to any node.
Note the node. clonenode (bool deep) method. When deep is set to true, it indicates the subnode and itself under the replication node; when deep is set to false,
Copy only yourself.
4. Delete a node Doc. documentelement. removechild (xmlnode node)

(Simple tasks, simple processing)



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.