Asp.net XML document to add the instance code for the deletion operation

  • 2020-05-07 19:29:15
  • OfStack

The XML file is called bcastr.xml

The structure 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 school of China pharmaceutical university " /> 
<item id="76" item_url="PicNews/Img/ Jiangning door .jpg" link="HTML/050/AI_20081017_50_53_76.html" itemtitle=" Photo news of graduate school " /> 
<item id="75" item_url="PicNews/Img/ China pharmaceutical university standard (perfect 2 ) .jpg" link="HTML/050/AI_20081017_50_53_75.html" itemtitle=" News test photo news " /> 
</bcaster> 

Function to add node:
 
///  To write the image news information to the image news player XML File set  
/// </summary> 
/// <param name="picpath"> Image path </param> 
/// <param name="htmlpath"> Photo news website </param> 
/// <param name="title"> The 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 the home page photo news no more than 5 article  
//{ 
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 the newly added photo news to the page 1 A position  
if (xnl.Count > 5) 
{ 
XmlNode lxn = rootnode.LastChild; 
rootnode.RemoveChild(lxn);// Delete the last image news  
} 


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

Function to modify the attributes of XML node:
 
/// <summary> 
///  Modify the XML attribute  
/// </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")); 
} 

Deletes the function of the specified XML node:
 
/// <summary> 
///  delete XML The specified 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)// If a node is deleted  
{ 
xe.RemoveAll(); 
break; 
} 
} 

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

Related articles: