使用根节点选择和使用文档对象选择节点有什么区别?哪种方式是首选的。
例如,
1.
XmlDocument Doc = new XmlDocument();
Doc.Load(mem);
XmlNodeList nodeList = Doc.SelectNodes(@"//@id");2.
XmlDocument Doc = new XmlDocument();
Doc.Load(mem);
XmlElement root = Doc.DocumentElement;
XmlNodeList nodeList = root.SelectNodes(@"//@id");发布于 2010-11-13 17:31:13
事实上,我从来没有得到过任何不同。并使用just
Doc.SelectNodes(@"//@id");因为如果文档的根存在
bool b = Doc.OuterXml == Doc.DocumentElement.OuterXml; // true发布于 2010-11-13 17:33:42
因为XPath的//表达式总是从文档根开始匹配,所以无论是从文档根还是从文档根documentElement开始,结果都是相同的。
所以我想你最好使用更短的Doc.SelectNodes("//@id");语法。
发布于 2010-11-14 05:53:36
XML文档的根至少包含其文档元素,但也可能包含处理指令和注释。例如,在这个XML文档中:
<!-- This is a child of the root -->
<document_element>
<!-- This is a child of the document element -->
<document_element>
<!-- This is also a child of the root -->根有三个子节点,其中一个是它的顶级元素。在本例中,如下所示:
XmlNodeList comments = doc.SelectNodes("comment()");还有这个:
XmlNodeList comments = doc.DocumentElement.SelectNodes("comment()");返回完全不同的结果。
https://stackoverflow.com/questions/4171839
复制相似问题