Instance code for adding and deleting operations in the Asp.net XML document

Source: Internet
Author: User
Tags net xml

The XML file name is bcastr. xml.

The structure is as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Bcaster>
<Item id = "79" item_url = "PicNews/Img/u = 404630538,2075277077" link = "HTML/050/AI_20081017_50_53_79.html" itemtitle = "111111111111111111"/>
<Item id = "78" item_url = "PicNews/Img/Index_04_01_06.jpg" link = "HTML/050/AI_20081017_50_53_78.html" itemtitle = "zengjia"/>
<Item id = "77" item_url = "PicNews/Img/bsxwf.jpg" link = "HTML/050/AI_20081017_50_53_77.html" itemtitle = "Graduate Department of China Pharmaceutical University"/>
<Item id = "76" item_url = "PicNews/Img/Jiangning Damen .jpg" link = "HTML/050/AI_20081017_50_53_76.html" itemtitle = ""/>
<Item id = "75" item_url = "PicNews/Img/Chinese Pharmaceutical University certification (perfect 22.16.jpg" link = "HTML/050/AI_20081017_50_53_75.html" itemtitle = "News test picture news"/>
</Bcaster>

Add node functions:
Copy codeThe Code is as follows:
/// Write the image news information to the XML file set of the image news player
/// </Summary>
/// <Param name = "picpath"> image path </param>
/// <Param name = "htmlpath"> image news URL </param>
/// <Param name = "title"> title </param>
Public void WritePicNewsXML (string picpath, string htmlpath, string title, string aid)
{
XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
// If (xnl. Count <= 5) // keep no more than 5 homepage pictures and news
//{
XmlNode rootnode = xmlDoc. SelectSingleNode ("bcaster ");

XmlElement fel = (XmlElement) rootnode. FirstChild;

XmlElement el = xmlDoc. CreateElement ("item"); // Add subnodes and attributes
El. SetAttribute ("id", aid );
El. SetAttribute ("item_url", picpath );
El. SetAttribute ("link", htmlpath );
El. SetAttribute ("itemtitle", title );
Rootnode. PrependChild (el); // Add the newly added image news to the first position.
If (xnl. Count> 5)
{
XmlNode lxn = rootnode. LastChild;
Rootnode. RemoveChild (lxn); // deletes the final picture news.
}


XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
//}
}

Function for modifying the attributes of an XML node:
Copy codeThe Code is as follows:
/// <Summary>
/// Modify XML attributes
/// </Summary>
/// <Param name = "picpath"> </param>
/// <Param name = "htmlpath"> </param>
/// <Param name = "title"> </param>
/// <Param name = "aid"> </param>
Public void EditPicNewsXML (string picpath, string htmlpath, string title, string aid)
{

XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. GetAttribute ("id") = aid) // rewrite if a node exists
{
Xe. SetAttribute ("item_url", picpath );
Xe. SetAttribute ("link", htmlpath );
Xe. SetAttribute ("itemtitle", title );
Break;
}
}

XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
}

Function used to delete a specified XML node:
Copy codeThe Code is as follows:
/// <Summary>
/// Delete the specified XML Node
/// </Summary>
/// <Param name = "aid"> </param>
Public void DelPicNewsXML (string aid)
{
XmlDocument xmlDoc;
XmlDoc = new XmlDocument ();
XmlDoc. Load (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));

XmlNodeList xnl = xmlDoc. SelectSingleNode ("bcaster"). ChildNodes;
Foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement) xn;
If (xe. GetAttribute ("id") = aid) // delete a node if it exists
{
Xe. RemoveAll ();
Break;
}
}

XmlDoc. Save (HttpContext. Current. Server. MapPath ("../PicNews/bcastr. xml "));
}

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.