asp.net C # Add delete find and modify XML file Element node

Source: Internet
Author: User
Tags foreach

An XML file (Bookstore.xml) is known as follows:

The code is as follows Copy Code

<?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 into the <bookstore> node:

The code is as follows Copy Code

XmlDocument xmldoc=new XmlDocument ();
Xmldoc.load ("Bookstore.xml");
XmlNode Root=xmldoc.selectsinglenode ("bookstore");//Find <bookstore>
XmlElement xe1=xmldoc.createelement ("book");//Create a <book> node
Xe1. SetAttribute ("Genre", "Li Zhanhong");/set the node genre property
Xe1. SetAttribute ("ISBN", "2-3631-4");//Set the node ISBN property

XmlElement xesub1=xmldoc.createelement ("title");
Xesub1. Innertext= "CS from getting started to proficient";/Set Text node
Xe1. AppendChild (XESUB1);//Add to <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 to <bookstore> node
Xmldoc.save ("Bookstore.xml");


The results are:

<?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 Zhanhong" isbn= "2-3631-4" >
<title>cs from getting started to mastering </title>
<author> Hou Jie </author>
<price>58.3</price>
</book>
</bookstore>


2, modify the node: The genre property value of "Li Zhanhong" node genre value changed to "Update Li Zhanhong", the node's child node <author> text modified to "Asia wins."

The code is as follows Copy Code

XmlNodeList Nodelist=xmldoc.selectsinglenode ("bookstore"). childnodes;//gets all the child nodes of the bookstore node
foreach (XmlNode xn in nodelist)//traverse all child nodes
{
XmlElement xe= (XmlElement) xn;//Converts a child node type to a XmlElement type
if (XE. GetAttribute ("genre") = = "Li Zhanhong")//If genre property value is "Li Zhanhong"
{
Xe. SetAttribute ("Genre", "Update Li Zhanhong");//Modify this property to "Update Li Zhanhong"

XmlNodeList Nls=xe. CHILDNODES;//continues to fetch all child nodes of the XE child node
foreach (XmlNode xn1 in NLS)//traversal
{
XmlElement xe2= (XmlElement) xn1;//conversion type
if (Xe2. name== "Author")//If found
{
Xe2. innertext= "Asia wins";//Modify
break;//find out and get out of here.
}
}
Break
}
}

Xmldoc.save ("Bookstore.xml"),//save.


The final results are:

<?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 Zhanhong" isbn= "2-3631-4" >
<title>cs from getting started to mastering </title>
<author> Asia wins </author>
<price>58.3</price>
</book>
</bookstore>

3, delete <book genre= "Fantasy" isbn= "2-3631-4" > Node genre properties, delete <book genre= "update Li Zhanhong" isbn= "2-3631-4" > node.

The code is as follows Copy Code


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 property
}
else if (XE. GetAttribute ("genre") = = "Update Li Zhanhong")
{
Xe. RemoveAll ()//delete all contents of the node
}
}
Xmldoc.save ("Bookstore.xml");

The final results are:

<?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 the data.

The code is as follows Copy Code

XmlNode Xn=xmldoc.selectsinglenode ("bookstore");
XmlNodeList Xnl=xn. ChildNodes;

foreach (XmlNode xnf in XNL)
{
XmlElement xe= (XmlElement) xnf;
Console.WriteLine (XE. GetAttribute ("genre"))//Display property value
Console.WriteLine (XE. GetAttribute ("ISBN"));

XmlNodeList Xnf1=xe. ChildNodes;
foreach (XmlNode xn2 in Xnf1)
{
Console.WriteLine (xn2. innertext);//Display child node point text
}
}

And then share an XML file Operation class

asp.net read, write, add, modify, delete operations on XML files
The following code debugging is correct

The code is as follows Copy Code

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Xml;
Private XmlDocument xmldoc;

Load XML file
private void Loadxml ()
{
Xmldoc=new XmlDocument ();
Xmldoc.load (Server.MapPath ("User.xml"));
}


Add node
private void AddElement ()
{

Loadxml ();

XmlNode xmldocselect=xmldoc.selectsinglenode ("user");

XmlElement el=xmldoc.createelement ("person"); Add Person node
El. SetAttribute ("Name", "Wind and Cloud"); Add the Person node's property ' name '
El.   setattribute ("Sex", "female"); Add the property "sex" of the person node
El.   SetAttribute ("Age", "25"); Add the property "age" of the person node

XmlElement xesub1=xmldoc.createelement ("Pass"); Add a node in the person node
Xesub1. innertext= "123";//Set Text node
El. AppendChild (XESUB1);
XmlElement xesub2=xmldoc.createelement ("address");
Xesub2. innertext= "Kunming";/Set Text node
El. AppendChild (XESUB2);

Xmldocselect.appendchild (EL);
Xmldoc.save (Server.MapPath ("User.xml"));


}


modifying nodes
private void Updateelement ()
{
Loadxml ();
XmlNodeList Nodelist=xmldoc.selectsinglenode ("user"). childnodes;//gets all the child nodes of the bookstore node
foreach (XmlNode xn in nodelist)//traverse all child nodes
{
XmlElement xe= (XmlElement) xn;//Converts a child node type to a XmlElement type
if (XE. GetAttribute ("name") = = "Cloud")//If the Name property value is "cloud"
{
Xe. SetAttribute ("name", "invention");


If there are child nodes down here
XmlNodeList Nls=xe. CHILDNODES;//continues to fetch all child nodes of the XE child node
foreach (XmlNode xn1 in NLS)//traversal
{
XmlElement xe2= (XmlElement) xn1;//conversion type
if (Xe2. name== "pass")//If found
{
Xe2. innertext= "66666";//Modify
Break

}
}

Break
}
}
Xmldoc.save (Server.MapPath ("User.xml"))//Save
}


Delete a node
private void Deletenode ()
{

Loadxml ();
XmlNodeList Xnl=xmldoc.selectsinglenode ("user"). ChildNodes;

foreach (XmlNode xn in xnl)
{
XmlElement xe= (XmlElement) xn;

if (XE. GetAttribute ("name") = = "Invention")
{
Xe. RemoveAttribute ("name");//Delete Name property
Xe. RemoveAll ()//delete all contents of the node
Xe. Parentnode.removechild (XE);

Break
}

}
Xmldoc.save (Server.MapPath ("User.xml"))//Save
}

private void Showit ()
{
Loadxml ();
XmlNode xn=xmldoc.selectsinglenode ("user");

XmlNodeList Xnl=xn. ChildNodes;

foreach (XmlNode xnf in XNL)
{
XmlElement xe= (XmlElement) xnf;
Console.WriteLine (XE. GetAttribute ("name"))//Display property value
Console.WriteLine (XE. GetAttribute ("Sex"));
//
XmlNodeList Xnf1=xe. ChildNodes;
foreach (XmlNode xn2 in Xnf1)
//                {
Console.WriteLine (xn2. innertext);//Display child node point text
//                }

}

}

The style of the XML:

<?xml version= "1.0" encoding= "gb2312"?>
<user>
<person>
</person>
<person name= "Wind Pull" sex= "male" age= ">"
<pass>123</pass>
<Address> daming </Address>
</person>
<person name= "The Wind and Cloud" sex= "female" age= ">"
<pass>123</pass>
<Address> Kunming </Address>
</person>
</user>

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.