如何删除XML的命名空间
如题:现有一个XML文件a.xml如下:
<?xml version="1.0" encoding="utf-8"?><root xmlns="http://www.163.com/rss/0.9"> <Header>头</Header> <Items> <Item> <Value>内容</Value> </Item> <Item> <Value>内容</Value> </Item> </Items> <Footer>脚</Footer></root>
using System;using System.IO;using System.Xml;public class Sample{ public static void Main() { XmlDocument doc = new XmlDocument(); doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" + "<title>Pride And Prejudice</title>" + "</book>"); XmlElement root = doc.DocumentElement; // Remove the genre attribute. root.RemoveAttribute("genre"); Console.WriteLine("Display the modified XML..."); Console.WriteLine(doc.InnerXml); }}
[解决办法]
当XML被 LODA到 内存中,是不能移除名空间xmlns的。所以上面的是去不掉xmlns的。
你可以通过替换里面的元素来实现了。
能不能在创建的时候就不加上xmlns呢?
------------------------------------
当然可以。
[解决办法]
把这个语句load到Document中,里面获取xml的字符串(doc.asXML()),然后强制把xmlns去掉,然后在加载再保存
不过这样比较麻烦。。。
最好的办法就是在你加载的时候就不要创建xmlns这个属性