C# 对xml 添加节点 删除节点

一克代码 2010-04-23 12:44:10
原来的一个xml
<?xml version="1.0" encoding="utf-8" ?>
<mmConfig>
<RedirectUrl>
<Url UrlName="1:IE累计补丁" UrlFile="1" />
<Url UrlName="2:系统累计补丁" UrlFile="2" />
<Url UrlName="3:URL跳转(请检查URL)" UrlFile="3" />
<Url UrlName="4:恶意软件删除工具" UrlFile="4" />
<Url UrlName="5:安装ActiveX 控件" UrlFile="5" />
<Url UrlName="6:Windows XP 更新程序" UrlFile="6" />
<Url UrlName="7:下载360安全卫士" UrlFile="7" />
<Url UrlName="8:雅虎助手" UrlFile="8" />
<Url UrlName="9:下载GoogleTalk" UrlFile="9" />
<Url UrlName="10:下载 Macromedia Flash Player" UrlFile="10" />
<Url UrlName="11:Web 迅雷" UrlFile="11" />
<Url UrlName="12:QQ升级下载" UrlFile="12" />
<Url UrlName="13:自定义(进入高级设置)" UrlFile="13" />
</RedirectUrl>
<ServerPack>
<sp SpName="RAR漏洞" SpFile="" />
<sp SpName="DOC漏洞" SpFile="" />
<sp SpName="PPT漏洞" SpFile="" />
</ServerPack>
</mmConfig>

现在需要对RedirectUrl节点添加<Url UrlName="14。添加的东西" UrlFile="14" />

也可以删除RedirectUrl里面的

操作后的xml
<?xml version="1.0" encoding="utf-8" ?>
<mmConfig>
<RedirectUrl>
<Url UrlName="1:IE累计补丁" UrlFile="1" />
<Url UrlName="2:系统累计补丁" UrlFile="2" />
<Url UrlName="3:URL跳转(请检查URL)" UrlFile="3" />
<Url UrlName="4:恶意软件删除工具" UrlFile="4" />
<Url UrlName="5:安装ActiveX 控件" UrlFile="5" />
<Url UrlName="6:Windows XP 更新程序" UrlFile="6" />
<Url UrlName="7:下载360安全卫士" UrlFile="7" />
<Url UrlName="8:雅虎助手" UrlFile="8" />
<Url UrlName="9:下载GoogleTalk" UrlFile="9" />
<Url UrlName="10:下载 Macromedia Flash Player" UrlFile="10" />
<Url UrlName="11:Web 迅雷" UrlFile="11" />
<Url UrlName="12:QQ升级下载" UrlFile="12" />
<Url UrlName="13:自定义(进入高级设置)" UrlFile="13" />
<Url UrlName="14。添加的东西" UrlFile="14" />
</RedirectUrl>
<ServerPack>
<sp SpName="RAR漏洞" SpFile="" />
<sp SpName="DOC漏洞" SpFile="" />
<sp SpName="PPT漏洞" SpFile="" />
</ServerPack>
</mmConfig>

求高手!!
...全文
210 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ffffssd 2010-04-24
  • 打赏
  • 举报
回复
支持,顶一下。
Net85 2010-04-24
  • 打赏
  • 举报
回复
插入数据得到你要的效果:
把try里面代码改为下面的试试,我试了试是可以:
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node); //查找node节点
XmlNodeList myNodes = doc.DocumentElement.GetElementsByTagName(node1);
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xe.SetAttribute(attribute1, value1);
XmlElement xel=(XmlElement) myNodes[0];
xel.AppendChild(xe);
doc.Save(path);

liuyileneal 2010-04-23
  • 打赏
  • 举报
回复
先获取RedirectUrl节点和 <Url UrlName="14。添加的东西" UrlFile="14" />节点

使用RemoveChild(XmlNode node)
插入用insertAfter()
无辜的程序员 2010-04-23
  • 打赏
  • 举报
回复
using XmlDocument load the file

using xPath get the target node

create the node u want add

append the node u create to the target node...

sorry, could not use chinese...
浮誇. 2010-04-23
  • 打赏
  • 举报
回复
学习的、、、
sunchen4359 2010-04-23
  • 打赏
  • 举报
回复
路过,学习了~~~
一克代码 2010-04-23
  • 打赏
  • 举报
回复
插入方法:
/// <summary>
/// 插入
/// </summary>
/// <param name="path"></param>
/// <param name="node"></param>
/// <param name="node1"></param>
/// <param name="element"></param>
/// <param name="attribute"></param>
/// <param name="value"></param>
public static void Insert(string path, string node, string node1, string element, string attribute, string value, string attribute1, string value1)
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node); //查找node节点
XmlElement xel = doc.CreateElement(node1);
//if (element.Equals(""))
//{
// if (!attribute.Equals(""))
// {
// XmlElement xe = (XmlElement)xe1;
// xe.SetAttribute(attribute, value);
// }
//}
//else
//{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
xe.InnerText = value;
else
xe.SetAttribute(attribute, value);
xe.SetAttribute(attribute1, value1);
xel.AppendChild(xe);
xn.AppendChild(xel);
//}
doc.Save(path);

}
catch { }
}
测试:Insert(xml, "mmConfig", "RedirectUrl", "Url", "UrlName", "小路工作室1231231231", "UrlFile","14");
但结果是
<?xml version="1.0" encoding="utf-8" ?>
<mmConfig>
<RedirectUrl>
<Url UrlName="1:IE累计补丁" UrlFile="1" />
<Url UrlName="2:系统累计补丁" UrlFile="2" />
<Url UrlName="3:URL跳转(请检查URL)" UrlFile="3" />
<Url UrlName="4:恶意软件删除工具" UrlFile="4" />
<Url UrlName="5:安装ActiveX 控件" UrlFile="5" />
<Url UrlName="6:Windows XP 更新程序" UrlFile="6" />
<Url UrlName="7:下载360安全卫士" UrlFile="7" />
<Url UrlName="8:雅虎助手" UrlFile="8" />
<Url UrlName="9:下载GoogleTalk" UrlFile="9" />
<Url UrlName="10:下载 Macromedia Flash Player" UrlFile="10" />
<Url UrlName="11:Web 迅雷" UrlFile="11" />
<Url UrlName="12:QQ升级下载" UrlFile="12" />
<Url UrlName="13:自定义(进入高级设置)" UrlFile="13" />
</RedirectUrl>
<ServerPack>
<sp SpName="RAR漏洞" SpFile="" />
<sp SpName="DOC漏洞" SpFile="" />
<sp SpName="PPT漏洞" SpFile="" />
</ServerPack>
- <RedirectUrl>
<Url UrlName="小路工作室222" UrlFile="14" />
</RedirectUrl>

</mmConfig>

它不插到我想要的地方!
看着乱
zhehan54 2010-04-23
  • 打赏
  • 举报
回复
AppendChild 插入节点
删除

XmlNodeList elemList = doc.DocumentElement.GetElementsByTagName("NodeName");

for(int i=0;i < elemList.Count;i++)
{
XmlNode node = elemList.Item(i);
node.RemoveAll();
}
wuyq11 2010-04-23
  • 打赏
  • 举报
回复
XmlElement xele= new XmlElement();
xele.SetAttribute("", "");
xele.Name="";
XmlDocument.AppendChild(xele);
XML 操作

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧