So I have some XML in the following format:
所以我有以下格式的XML:
<somenode>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title/>
</head>
<body>
<p>P one</p>
<p>Another p</p>
</body>
</html>
</somenode>
Nestled in there is some html, which I didn't think would be an issue as it would just be treated as xml.
坐在那里有一些html,我认为不会是一个问题,因为它只会被视为xml。
I'm trying to select the contents (InnerXml) of the <body> tag. However, using
我正在尝试选择标签的内容(InnerXml)。但是,使用
xmlDoc.SelectSingleNode("somenode/html/body")
returns null
, and using
返回null,并使用
xmlDoc.GetElementsByTagName("body")[0].InnerXml
gives the InnerXml - but each <p> has xmlns="http://www.w3.org/1999/xhtml"
appended to it - so the result looks like:
给出了InnerXml - 但是每个
都附加了xmlns =“http://www.w3.org/1999/xhtml” - 所以结果如下:
<p xmlns="http://www.w3.org/1999/xhtml">P one</p><p xmlns="http://www.w3.org/1999/xhtml">Another p</p>
Can anyone shed some light on this? Seems like some really weird behavior, any help would be appreciated. I'm only using ASP.net 2.0, so unfortunately trying linq isn't an option.
任何人都可以对此有所了解吗?看起来像一些非常奇怪的行为,任何帮助将不胜感激。我只使用ASP.net 2.0,所以不幸的是尝试linq不是一个选择。
2 个解决方案
#1
0
Your xpath expression isn't specifying the default namespace. How about:
您的xpath表达式未指定默认命名空间。怎么样:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
XmlNode node = xmlDoc.SelectSingleNode("somenode/xhtml:html/xhtml:body", nsMgr);
#2
0
Since the <html>
element defines the default namespace to be http://www.w3.org/1999/xhtml. All elements inside it without a namespace prefix have the same namespace by default.
由于元素将默认命名空间定义为http://www.w3.org/1999/xhtml。默认情况下,没有名称空间前缀的所有元素都具有相同的名称空间。
Since the content of the body tag is 2 separate <p>
elements, they both get the declaration. If you had other elements inside your <p>
elements, they will not have the declaration on them.
由于body标签的内容是2个单独的
元素,因此它们都获得声明。如果
元素中有其他元素,则它们将不具有声明。
#1
0
Your xpath expression isn't specifying the default namespace. How about:
您的xpath表达式未指定默认命名空间。怎么样:
XmlNamespaceManager nsMgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsMgr.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
XmlNode node = xmlDoc.SelectSingleNode("somenode/xhtml:html/xhtml:body", nsMgr);
#2
0
Since the <html>
element defines the default namespace to be http://www.w3.org/1999/xhtml. All elements inside it without a namespace prefix have the same namespace by default.
由于元素将默认命名空间定义为http://www.w3.org/1999/xhtml。默认情况下,没有名称空间前缀的所有元素都具有相同的名称空间。
Since the content of the body tag is 2 separate <p>
elements, they both get the declaration. If you had other elements inside your <p>
elements, they will not have the declaration on them.
由于body标签的内容是2个单独的
元素,因此它们都获得声明。如果
元素中有其他元素,则它们将不具有声明。