http://blog.csdn.net/zth603/article/details/5743880
C # Add, modify, delete XML nodes
Add an XML node
private void Addxml (string image, string title)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (Server.MapPath (". /flash/dati.xml "));
XmlNode root = Xmldoc.selectsinglenode ("Images");//Find <images>
XmlElement xe1 = xmldoc.createelement ("thumb");//Create a <thumb> node
Xe1. SetAttribute ("Displaynum", "6");//Set the node Displaynum property
Xe1. SetAttribute ("Separation", "5");//Set the node separation property XmlElement Xesub1 = Xmldoc.createelement ("image");
Xesub1. InnerText = image;//Set Text node
Xe1. AppendChild (XESUB1);//Add to the Thumb node
XmlElement xesub2 = xmldoc.createelement ("description");
Xesub2. InnerText = title;
Xe1. AppendChild (XESUB2); Root. AppendChild (XE1);//Add to <images> node
Xmldoc.save (Server.MapPath (". /flash/dati.xml "));
}//Delete node contents
private void Delxml (string image)
{
XmlDocument xmldoc = new XmlDocument ();
Xmldoc.load (Server.MapPath (". /flash/dati.xml "));
XmlNodeList XNL = Xmldoc.selectsinglenode ("Images"). ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement XE = (XmlElement) xn;
if (XE. Innertext.indexof (image,0) >= 0)
{
Xn. Parentnode.removechild (xn);
Xn. RemoveAll ();
}
}
Xmldoc.save (Server.MapPath (". /flash/dati.xml "));
}//Modify foreach (XmlNode xn in nodeList)//traverse all child nodes
{
XmlElement xe= (XmlElement) xn;//to convert child node types to XmlElement types
if (XE. GetAttribute ("genre") = = "")//interpretation conditions
{
Xe. SetAttribute ("Genre", newstr);//Modify this property to Newstr
XmlNodeList Nls=xe. childnodes;//continue 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== "Author")//If found//interpretation conditions
{
Xe2. innertext=newtext;//is modified
break;//find out, you can do it.
}
}
Break
}
C # Add, modify, delete XML node excerpt