Asp.net XML文档进行添加删改操作的实例代码
程序员文章站
2024-03-09 10:17:47
xml文件名为bcastr.xml 结构如下: 复制代码 代码如下:
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"));
}
结构如下:
复制代码 代码如下:
<?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"));
}
上一篇: discuz 跨域整合的记录文件
下一篇: CentOS8的基础防火墙配置