C # Add, modify, delete XML node excerpt

Source: Internet
Author: User

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

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.