最近一次在C#中用XMLDocument添加元素,遇 到了这样一个问题:
当根节点具有 xmlns 属性时,用 XMLDocument 创建子元素时如果不指定 xmlns 或指定 xmlns 为 null 时,子元素将自动具有 xmlns="" 属性,很是烦人。

后来发现了问题原因:
当父节点具有 xmlns 属性时,子节点必须指定 xmlns 属性,仅当子节点的 xmnls 属性与父节点相同时,子节点才不显示 xmlns 属性,最终就不会在 .xml 文件中显示出来。

解决办法:

XmlDocument xd = new XmlDocument();
//注意 CreateElement 方法的第二个参数。 
XmlElement url = xd.CreateElement("url",xd.DocumentElement.NamespaceURI);

 

注意:在每一个下级节点,都要继续指定命名空间,否则仍会出现 xmlns="" 属性。

相关文章:

  • 2022-12-23
  • 2022-02-20
  • 2021-09-13
  • 2022-12-23
  • 2021-08-04
  • 2021-09-10
  • 2021-09-24
猜你喜欢
  • 2022-12-23
  • 2021-07-13
  • 2022-12-23
  • 2021-07-20
  • 2022-12-23
  • 2021-09-13
  • 2022-12-23
相关资源
相似解决方案