asp. net operation xml add delete and change sample share
- 2020-11-26 18:45:40
- OfStack
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 a 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 person Properties of nodes "name"
el.SetAttribute("sex"," female "); // add person Properties of nodes "sex"
el.SetAttribute("age","25"); // add person Properties of nodes "age"
XmlElement xesub1=xmlDoc.CreateElement("pass"); // add person A node within a node
xesub1.InnerText="123"; // Set the text node
el.AppendChild(xesub1);
XmlElement xesub2=xmlDoc.CreateElement("Address");
xesub2.InnerText=" kunming "; // Set the text node
el.AppendChild(xesub2);
xmldocSelect.AppendChild(el);
xmlDoc.Save(Server.MapPath("user.xml"));
}
// Modify the node
private void UpdateElement()
{
LoadXml();
XmlNodeList nodeList=xmlDoc.SelectSingleNode("user").ChildNodes; // To obtain bookstore All the children of a node
foreach(XmlNode xn in nodeList) // Iterate over all the child nodes
{
XmlElement xe=(XmlElement)xn; // Converts the child node type to XmlElement type
if(xe.GetAttribute("name")==" Wind and cloud ") // if name Attribute value is "Fengyun"
{
xe.SetAttribute("name"," The invention "); // If there are children down there
XmlNodeList nls=xe.ChildNodes;// Continue to get xe All of the child nodes
foreach(XmlNode xn1 in nls)// traverse
{
XmlElement xe2=(XmlElement)xn1;// Conversion type
if(xe2.Name=="pass")// If you find
{
xe2.InnerText="66666";// The modified
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")==" The invention ")
{
//xe.RemoveAttribute("name");// delete name attribute
xe.RemoveAll();// Deletes all of the 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 values
// Console.WriteLine(xe.GetAttribute("sex"));
// // XmlNodeList xnf1=xe.ChildNodes;
// foreach(XmlNode xn2 in xnf1) //
{
// Console.WriteLine(xn2.InnerText);
// Display child node dot text
// }
}
}
Xml style:
<?xml version="1.0" encoding="gb2312"?>
<user>
<person> </person>
<person name=" The wind, " sex=" male " age="25">
<pass>123</pass>
<Address> daming </Address>
</person>
<person name=" Wind and cloud " sex=" female " age="25">
<pass>123</pass>
<Address> kunming </Address>
</person>
</user>