XML檔案名稱為bcastr.xml
結構如下: 複製代碼 代碼如下:<?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="中國藥科大學研究生部" />
<item id="76" item_url="PicNews/Img/江寧大門.jpg" link="HTML/050/AI_20081017_50_53_76.html" itemtitle="研究生部圖片新聞" />
<item id="75" item_url="PicNews/Img/中國藥科大學校標(完美2).jpg" link="HTML/050/AI_20081017_50_53_75.html" itemtitle="新聞測試圖片新聞" />
</bcaster>
添加節點的函數: 複製代碼 代碼如下:/// 將圖片新聞資訊寫入到圖片新聞播放器的XML檔案集中
/// </summary>
/// <param name="picpath">圖片路徑</param>
/// <param name="htmlpath">圖片新聞網址</param>
/// <param name="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)//保持首頁圖片新聞不超過5條
//{
XmlNode rootnode = xmlDoc.SelectSingleNode("bcaster");
XmlElement fel = (XmlElement)rootnode.FirstChild;
XmlElement el = xmlDoc.CreateElement("item");//添加子節點及屬性
el.SetAttribute("id", aid);
el.SetAttribute("item_url", picpath);
el.SetAttribute("link", htmlpath);
el.SetAttribute("itemtitle", title);
rootnode.PrependChild(el);//將新加入的圖片新聞添加到第一個位置
if (xnl.Count > 5)
{
XmlNode lxn = rootnode.LastChild;
rootnode.RemoveChild(lxn);//刪除最末尾的圖片新聞
}
xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));
//}
}
修改XML節點的屬性的函數: 複製代碼 代碼如下:/// <summary>
/// 修改XML屬性
/// </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)//如果節點存在則改寫
{
xe.SetAttribute("item_url", picpath);
xe.SetAttribute("link", htmlpath);
xe.SetAttribute("itemtitle", title);
break;
}
}
xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));
}
刪除指定的XML節點的函數: 複製代碼 代碼如下:/// <summary>
/// 刪除XML指定節點
/// </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)//如果節點存在刪除
{
xe.RemoveAll();
break;
}
}
xmlDoc.Save(HttpContext.Current.Server.MapPath("../PicNews/bcastr.xml"));
}