Use xmldocument to operate XML files

Source: Internet
Author: User

Xmldocument can be used to conveniently operate XML files.

1. Basic Methods for operating XML files

(1) Add a reference to system. xml and use the using statement to add a reference;

(2) assume that the XML file to be read is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Students>
<Student>
<Name> Zhang liangjing </Name>
<Age> 20 </age>
<Holobby> singing songs </Student>
<Student>
<Name> Zhou jiejie </Name>
<Age> 22 </age>
<Holobby> play with a dual-stick </Student>
</Students>

(3) write programs and add nodes:

Private Static void addnode ()
{
Xmldocument myxml = new xmldocument ();
Myxml. Load ("student. xml"); // read the specified XML document
Xmlnode students = myxml. selectsinglenode ("Students"); // read students Node
Xmlelement newstudent = myxml. createelement ("student ");

Newstudent. setattribute ("class", "10 software ");
Xmlelement name = myxml. createelement ("name ");
Name. innertext = "James ";
Xmlelement age = myxml. createelement ("Age ");
Age. innertext = "20 ";
Xmlelement holobby = myxml. createelement ("holobby ");
Holobby. innertext = "reading ";
Newstudent. appendchild (name );
Newstudent. appendchild (AGE );
Newstudent. appendchild (holobby );
Students. appendchild (newstudent );
Myxml. Save ("student. xml ");
}

(4) Delete A node:

Private Static void delnode ()
{
Xmldocument myxml = new xmldocument ();
Myxml. Load ("student. xml"); // read the specified XML document
Xmlnode student = myxml. selectsinglenode ("// student [name = 'zhang deyou ']"); // read the node
If (student! = NULL)
Student. parentnode. removechild (student );
Myxml. Save ("student. xml ");
}

(5) modify the node value:

Private Static void changenode ()
{
Xmldocument myxml = new xmldocument ();
Myxml. Load ("student. xml"); // read the specified XML document
Xmlnodelist students = myxml. documentelement. childnodes;
Foreach (xmlnode student in students)
{
Xmlelement name = student. selectsinglenode ("name") as xmlelement; // read a node
If (name. innertext. Equals ("Zhang San "))
Name. innertext = "Li Si ";
}
Myxml. Save ("student. xml ");
}

(6) read XML files:

Private Static void readnode ()
{
Xmldocument myxml = new xmldocument ();
Myxml. Load ("student. xml"); // read the specified XML document
Xmlnodelist students = myxml. documentelement. childnodes; // read all student nodes
Foreach (xmlnode student in students) // loop the subnode
{
Foreach (xmlnode node in student. childnodes)
{
// Display the content of each node
Switch (node. Name)
{
Case "name ":
Console. writeline ("Name: {0}", node. innertext );
Break;
Case "Age ":
Console. writeline ("Age: {0}", node. innertext );
Break;
Case "holobby ":
Console. writeline ("Hobbies: {0}", node. innertext );
Break;
}
}
}
}

2. Operate the ASP. NET Website map file:

The format of the website map file web. sitemap is as follows:

<? XML version = "1.0" encoding = "UTF-8"?>
<Sitemapnode url = "default. aspx" Title = "New Book recommendation" Description = "">
<Sitemapnode Title = "C #" url = "Booklist. aspx? Typeid = 1 "Description =" "/>
<Sitemapnode Title = ". Net" url = "Booklist. aspx? Typeid = 25 "Description =" "/>
</Sitemapnode>

/// <Summary>
/// Update all XML
/// </Summary>
Private void modifyxml ()
{
Xmldoctree. Load (server. mappath (catagoryxml ));
Xmldoctree. selectsinglenode ("sitemapnode"). innertext = "";
Foreach (Category catagory in categorymanager. getallcategories ())
{
Makechild (catagory );
}
Xmldoctree. Save (server. mappath (catagoryxml ));
}
/// <Summary>
/// Set the subnode
/// </Summary>
/// <Param name = "catagory"> </param>
Private void makechild (Category catagory)
{
Xmlelement xmlelnode = xmldoctree. createelement ("sitemapnode ");
Xmlelnode. setattribute ("title", catagory. Name );
Xmlelnode. setattribute ("url", booklisturl + catagory. Id. tostring ());
Xmlelnode. setattribute ("Description ","");
Xmldoctree. selectsinglenode ("sitemapnode"). appendchild (xmlelnode );
}

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.