XML file read/write, add, modify, delete operation

Source: Internet
Author: User
Tags foreach

XML file read/write, add, modify, delete operation
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;//get all child nodes of 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 acquire 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
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
//                }

}

       }
   
XML style:
<?xml version= "1.0" encoding= "gb2312"
<user>
  < Person>
  </person>
  <person name= "Wind Pull" sex= "man" 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>

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.