C # getting started with operating XML files (with the example source code below)

Source: Internet
Author: User

It is known that there is an XML file (bookstore. XML) as follows:

<? XML version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "Fantasy" ISBN = "2-3631-4">
<Title> Oberon's legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
</Bookstore>

1. Insert a <book> node to the <bookstore> node:

Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load ("Bookstore. xml ");
Xmlnode root = xmldoc. selectsinglenode ("Bookstore"); // query <bookstore>
Xmlelement xe1 = xmldoc. createelement ("book"); // create a <book> node
Xe1.setattribute ("genre", "lizan red"); // you can specify the genre attribute of the node.
Xe1.setattribute ("ISBN", "2-3631-4"); // you can specify the ISBN attribute of the node.

Xmlelement xesub1 = xmldoc. createelement ("title ");
Xesub1.innertext = "Cs from entry to entry"; // set a text node
Xe1.appendchild (xesub1); // Add it to the <book> node
Xmlelement xesub2 = xmldoc. createelement ("author ");
Xesub2.innertext = "Hou Jie ";
Xe1.appendchild (xesub2 );
Xmlelement xesub3 = xmldoc. createelement ("price ");
Xesub3.innertext = "58.3 ";
Xe1.appendchild (xesub3 );

Root. appendchild (xe1); // Add it to the <bookstore> node
Xmldoc. Save ("Bookstore. xml ");

// ==========================
Result:

 

<? XML version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "Fantasy" ISBN = "2-3631-4">
<Title> Oberon's legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "Li zanhong" ISBN = "2-3631-4">
<Title> Cs from entry to entry </title>
<Author> Hou Jie </author>
<Price> 58.3 </price>
</Book>
</Bookstore>

 

 

2. Modify the node: Change the genre value of the node whose genre attribute value is "Li zenhong" to "Update Li zenhong ", modify the text of the child node <author> of the node to "Yasheng ".

Xmlnodelist nodelist = xmldoc. selectsinglenode ("Bookstore"). childnodes; // obtain all the subnodes of the bookstore Node
Foreach (xmlnode Xn in nodelist) // traverses all subnodes
{
Xmlelement Xe = (xmlelement) xn; // converts the subnode type to the xmlelement type
If (Xe. getattribute ("genre") = "") // If the genre attribute value is ""
{
Xe. setattribute ("genre", "Update lizan red"); // you can change this attribute to "Update lizan red"

Xmlnodelist NLS = Xe. childnodes; // continue to obtain all the child nodes of the Xe subnode
Foreach (xmlnode xn1 in NLS) // traverse
{
Xmlelement xe2 = (xmlelement) xn1; // Conversion Type
If (xe2.name = "author") // If you find
{
Xe2.innertext = "Yasheng"; // modify
Break; // find and exit.
}
}
Break;
}
}

Xmldoc. Save ("Bookstore. xml"); // save.

 

// ==============================

The final result is:

<? XML version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book genre = "Fantasy" ISBN = "2-3631-4">
<Title> Oberon's legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book genre = "Update Li zanhong" ISBN = "2-3631-4">
<Title> Cs from entry to entry </title>
<Author> Yasheng </author>
<Price> 58.3 </price>
</Book>
</Bookstore>

 

3. Delete the genre attribute of the <book genre = "Fantasy" ISBN = "2-3631-4"> node, delete the <book genre = "Update lizan red" ISBN = "2-3631-4"> node.

Xmlnodelist xnl = xmldoc. selectsinglenode ("Bookstore"). childnodes;

Foreach (xmlnode Xn in xnl)
{
Xmlelement Xe = (xmlelement) xn;

If (Xe. getattribute ("genre") = "Fantasy ")
{
Xe. removeattribute ("genre"); // Delete genre attributes
}
Else if (Xe. getattribute ("genre") = "Update lizanhong ")
{
Xe. removeall (); // delete all content of the node
}
}
Xmldoc. Save ("Bookstore. xml ");

// ==================================

The final result is:

<? XML version = "1.0" encoding = "gb2312"?>
<Bookstore>
<Book ISBN = "2-3631-4">
<Title> Oberon's legacy </title>
<Author> corets, Eva </author>
<Price> 5.95 </price>
</Book>
<Book>
</Book>
</Bookstore>

4. display all data.

Xmlnode xn = xmldoc. selectsinglenode ("Bookstore ");

Xmlnodelist xnl = xn. childnodes;

Foreach (xmlnode xnf in xnl)
{
Xmlelement Xe = (xmlelement) xnf;
Console. writeline (Xe. getattribute ("genre"); // display attribute values
Console. writeline (Xe. getattribute ("ISBN "));

Xmlnodelist xnf1 = Xe. childnodes;
Foreach (xmlnode xn2 in xnf1)
{
Console. writeline (xn2.innertext); // displays the child node text.
}
}

Reference: http://www.knowsky.com/340889.html

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

 

Note:

In the reply on the third floor, I made an example to demonstrate how to add a subnode to a node, if you are interested, you can try other operations later.

Download Demo: testxmlnodeappend.rar

--- Khler

2008.12.09

 

 

 

 

 

 

 

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.