xmlns 添加XML节点时不想加这个节点

guoweijun394 2007-05-09 11:16:59
我添加一个XML 节点时 总是自动加这个xmlns属性
xe.RemoveAttribute("xmlns");也没删除 ,怎么不加呢?
...全文
366 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovefootball 2007-05-09
  • 打赏
  • 举报
回复
我跑了你的Source没有问题
你删除后没有保存~~~~~
guoweijun394 2007-05-09
  • 打赏
  • 举报
回复
//先建立一个XML DOM
XmlDocument doc = new XmlDocument();
//读入XML文件
doc.Load(".\\Config\\Services.config");
//建立一个节点。
XmlElement newService = doc.CreateElement("Service");

//建立一个Name属性,并将次属性放置在接点 “newService” 下
XmlAttribute newName = doc.CreateAttribute("Name");
newName.InnerText = "abc";
newService.SetAttributeNode(newName);
//保存
doc.DocumentElement.FirstChild.AppendChild(newService);
doc.Save(".\\Config\\Services.config");

这是添加的代码
删除代码

XmlNodeList nodeList = doc.GetElementsByTagName("Services");

//遍历所有子节点
foreach (XmlNode xn in nodeList)
{

foreach (XmlNode cxm in xn.ChildNodes)
{
XmlElement xe = (XmlElement)cxm;
xe.RemoveAttribute("xmlns");
}
}

lovefootball 2007-05-09
  • 打赏
  • 举报
回复
你的source
guoweijun394 2007-05-09
  • 打赏
  • 举报
回复
好了 多谢你了 (: 分有点少 呵呵 就这么点分了
lovefootball 2007-05-09
  • 打赏
  • 举报
回复

是你命名空间的问题

doc.DocumentElement.FirstChild.AppendChild(newService);
--〉
doc.DocumentElement.FirstChild.InnerXml += newService.OuterXml;

不要直接AppendChild
这样就不会把命名空间加上去了
xray2005 2007-05-09
  • 打赏
  • 举报
回复
学习ing
wuxing2006 2007-05-09
  • 打赏
  • 举报
回复
xmlns 是保留字,是命名空间,不是属性
guoweijun394 2007-05-09
  • 打赏
  • 举报
回复
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
<Services>
<Service Name="abc" xmlns="" />
</Services>
</configuration>

和<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"> 是不有关呢
lovefootball 2007-05-09
  • 打赏
  • 举报
回复
你把你的XML文件贴上来
我添加节点的时候没有xmlns
guoweijun394 2007-05-09
  • 打赏
  • 举报
回复
lovefootball 你忙吗?帮我再看下
谢谢
guoweijun394 2007-05-09
  • 打赏
  • 举报
回复
呵呵 我怎么删除不了呢 保存了
....

doc.DocumentElement.FirstChild.AppendChild(newService);
//保存
XmlNodeList nodeList = doc.GetElementsByTagName("Services");
//遍历所有子节点
foreach (XmlNode xn in nodeList)
{

foreach (XmlNode cxm in xn.ChildNodes)
{
XmlElement xe = (XmlElement)cxm;
xe.RemoveAttribute("xmlns");
}
}
doc.Save(".\\Config\\Services.config");

110,571

社区成员

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

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

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