ASP.net An example code for adding a pruning operation to an XML document _ Practical skills

Source: Internet
Author: User
Tags httpcontext
XML file name is Bcastr.xml

The structure is as follows:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<bcaster>
<item id= "item_url=" "picnews/img/u=404630538,2075277077" link= "html/050/ai_20081017_50_53_79.html" Itemtitle = "111111111111111111"/>
<item id= "item_url=" picnews/img/index_04_01_06.jpg "link=" html/050/ai_20081017_50_53_78.html "itemtitle=" Zengjia "/>
<item id= "item_url=" "Picnews/img/bsxwf.jpg" link= "html/050/ai_20081017_50_53_77.html" itemtitle= "graduate Department of China Pharmaceutical University" />
<item id= "item_url=" picnews/img/Jiangning gate. jpg "link=" html/050/ai_20081017_50_53_76.html "itemtitle=" Photo News of Graduate Department >
<item id= "item_url=" picnews/img/China Pharmaceutical University School mark (Perfect 2). jpg "link=" html/050/ai_20081017_50_53_75.html "itemtitle=" News test Picture News "/>
</bcaster>

To add a node's function:
Copy Code code as follows:

Write picture news information to the image News Player's XML file set
</summary>
<param name= "Picpath" > Picture path </param>
<param name= "Htmlpath" > Photo 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 home Picture news no more than 5
//{
XmlNode RootNode = Xmldoc.selectsinglenode ("Bcaster");

XmlElement fel = (xmlelement) rootnode. FirstChild;

XmlElement el = xmldoc.createelement ("item");//Add child nodes and properties
El. SetAttribute ("id", aid);
El. SetAttribute ("Item_url", Picpath);
El. SetAttribute ("link", Htmlpath);
El. SetAttribute ("Itemtitle", title);
RootNode. PrependChild (EL)//Add new picture News to the first location
if (XNL. Count > 5)
{
XmlNode lxn = RootNode. LastChild;
RootNode. RemoveChild (LXN)//Delete the end of the picture news
}


Xmldoc.save (HttpContext.Current.Server.MapPath) (". /picnews/bcastr.xml "));
//}
}

function to modify the attributes of an XML node:
Copy Code code as follows:

<summary>
modifying 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)//overwrite if 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 "));
}

To delete a function of the specified XML node:
Copy Code code as follows:

///<summary>
///Delete xml specify node
///</summary>
///& Lt;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)//If the node exists delete
{
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.