SoFunction
Updated on 2025-03-07

Example code for adding, deleting and modifying XML documents

The XML file name is

The structure is as follows:
Copy the codeThe code is as follows:

<?xml version="1.0" encoding="utf-8"?>
<bcaster>
<item item_url="PicNews/Img/u=404630538,2075277077" link="HTML/050/AI_20081017_50_53_79.html" itemtitle="111111111111111111" />
<item item_url="PicNews/Img/Index_04_01_06.jpg" link="HTML/050/AI_20081017_50_53_78.html" itemtitle="zengjia" />
<item item_url="PicNews/Img/" link="HTML/050/AI_20081017_50_53_77.html" itemtitle="Graduate Department of China Pharmaceutical University" />
<item item_url="PicNews/Img/Jiangning Gate.jpg" link="HTML/050/AI_20081017_50_53_76.html" itemtitle="Graduate School Department Picture News" />
<item item_url="PicNews/Img/China Pharmaceutical University School Standard (Perfect 2).jpg" link="HTML/050/AI_20081017_50_53_75.html" itemtitle="News Test Picture News" />
</bcaster>

Functions to add nodes:
Copy the codeThe code is as follows:

/// Write image news information to the XML file set of the image news player
/// </summary>
/// <param name="picpath">Picture path</param>
/// <param name="htmlpath">Picture News Website</param>
/// <param name="title">Title</param>
public void WritePicNewsXML(string picpath, string htmlpath, string title,string aid)
{
XmlDocument xmlDoc;
xmlDoc = new XmlDocument();
(("../PicNews/"));

XmlNodeList xnl = ("bcaster").ChildNodes;
//if ( <= 5)//Keep no more than 5 pictures and news on the homepage
//{
XmlNode rootnode = ("bcaster");

XmlElement fel = (XmlElement);

XmlElement el = ("item");//Add child nodes and attributes
("id", aid);
("item_url", picpath);
("link", htmlpath);
("itemtitle", title);
(el);//Add newly added image news to the first location
if ( > 5)
{
XmlNode lxn = ;
(lxn);//Delete the last picture news
}


(("../PicNews/"));
//}
}

Functions that modify the properties of XML nodes:
Copy the codeThe code is as follows:

/// <summary>
/// Modify XML properties
/// </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();
(("../PicNews/"));

XmlNodeList xnl = ("bcaster").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (("id") == aid)//Rewrite if the node exists
{
("item_url", picpath);
("link", htmlpath);
("itemtitle", title);
break;
}
}

(("../PicNews/"));
}

Functions that delete the specified XML node:
Copy the codeThe code is as follows:

/// <summary>
/// Delete XML specified node
/// </summary>
/// <param name="aid"></param>
public void DelPicNewsXML(string aid)
{
XmlDocument xmlDoc;
xmlDoc = new XmlDocument();
(("../PicNews/"));

XmlNodeList xnl = ("bcaster").ChildNodes;
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
if (("id") == aid)//If the node exists
{
();
break;
}
}

(("../PicNews/"));
}