我有XHTML文件,它的开头是:
<html xmlns="http://www.w3.org/1999/xhtml">我装载它:
XmlDocument xml = new XmlDocument();
StringReader sr = new StringReader(html);
XmlTextReader xmltr = new XmlTextReader(sr);
xmltr.Namespaces = false;
xml.Load(xmltr);当我调用xml.InnerXml时,我总是得到The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'.异常,无法访问The 'xmlns' attribute is bound to the reserved namespace 'http://www.w3.org/2000/xmlns/'.的内部xml。如何在加载过程中摆脱xmlns?
解决办法是:
XmlNode xmln = xml.SelectSingleNode("//html");
if (xmln != null)
((XmlElement)xmln).RemoveAttribute("xmlns");发布于 2012-01-27 14:33:46
据猜测,您试图解析的页面最近已更改为XHTML,因此出现了命名空间?
如@JonSkeet所示,您不应该在xmltr.Namespaces = false;上设置XmlTextReader
你可以
xmlns="http://www.w3.org/1999/xhtml")命名空间。xpath (如local-name() ),它将忽略名称空间:*
xml.SelectSingleNode("/*[local-name()='html']/*[local-name()='body']")无论哪种方式,您的代码都需要更改以适应名称空间,除非您在加载XML之前将名称空间从XML中破解出来。
*也可以与local-name()一起使用//,但是要小心使用包含大量元素的文档--这可能会变得非常慢。
https://stackoverflow.com/questions/9032493
复制相似问题