问题描述
当我想遍历使用XPath我的XmlDocument,我来到你们这个问题,有文档在许多丑陋的命名空间,所以我就开始使用 NamespaceManager
随XPath的。
When I want to traverse my XmlDocument using XPath, I came unto the problem that there were many ugly namespaces in the document, so I started using a NamespaceManager
along with the XPath.
中的XML看起来像这样
The XML looks like this
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="KA0100401">
<Table>
<Row>
<Cell>Data</Cell>
</Row>
<!-- more rows... -->
</Table>
</Worksheet>
<Worksheet ss:Name="KA0100402">
<!-- .... --->
</Worksheet>
</Workbook>
现在,从我从这份文件看,瓮:架构 - 微软COM:办公室:■preadsheet
是默认的命名空间,因为它位于根元素。
Now, from what I see from this document, "urn:schemas-microsoft-com:office:spreadsheet"
is the default namespace, because it sits on the root element.
所以,天真地,我配置了我的 NamespaceManager
是这样的:
So, naively, I configured my NamespaceManager
like this:
XmlDocument document = new XmlDocument();
document.Load(reader);
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace(String.Empty, "urn:schemas-microsoft-com:office:spreadsheet");
manager.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
manager.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
manager.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
manager.AddNamespace("html", "http://www.w3.org/TR/REC-html40");
但是,当我试图访问一个节点
But, when I try to access a node
foreach (XmlNode row in document.SelectNodes("/Workbook/Worksheet[1]/Table/Row", manager))
我从来没有得到任何结果。我下的是IM pression,通过设置第一个命名空间和一个空preFIX,我不需要,当搜索在工作区中的节点来设置。
I never get any results. I was under the impression that by setting the first namespace with an empty prefix, I wouldn't need to set that when searching for nodes in that workspace.
但是,因为它是表示对 AddNamespace
方法:
But, as it is stated on the AddNamespace
method:
如果一个XPath前pression不包括preFIX,假定命名空间统一资源标识符(URI)是空的空间。
这是为什么?而且,更重要的是:?如何访问在默认命名空间节点,如果不使用preFIX设置成一个空的空间
Why is that? And, more important: How do I access nodes in the default namespace, if not using a prefix sets them into an empty namespace?
什么是好上设置管理器的默认命名空间,如果搜索节点时,我甚至不能访问它?
What good is setting the default namespace on the manager if I can't even access it when searching for nodes?
推荐答案
@ JLRishe的答案是正确的访问节点的默认命名空间(即总是在的XmlNamespaceManager )。
@JLRishe's answer is correct for accessing nodes in the default namespace (ie. always mapping a prefix to the default namespace in the XmlNamespaceManager
).
读链路的整个范围内从报价()中指出,在默认的空preFIX没有在XPath的前pressions使用。
Reading the entire context of the link from your quote (MSDN XmlNamespaceManager.AddNamespace) it is stated that the default "empty" prefix is not used in XPath expressions.
preFIX 类型:System
在preFIX与被添加的命名空间相关联。使用的String.Empty添加默认的命名空间。>
The prefix to associate with the namespace being added. Use String.Empty to add a default namespace.>
请注意如果XmlNamespaceManager的将用于在XML路径语言解析名称空间(的XPath)EX pression,一个preFIX必须指定。如果一个XPath前pression不包括preFIX,假定命名空间统一资源标识符(URI)是空的空间。有关XPath的EX pressions和XmlNamespaceManager的更多信息,请参阅XmlNode.SelectNodes和XPathEx pression.SetContext方法。
Note If the XmlNamespaceManager will be used for resolving namespaces in an XML Path Language (XPath) expression, a prefix must be specified. If an XPath expression does not include a prefix, it is assumed that the namespace Uniform Resource Identifier (URI) is the empty namespace. For more information about XPath expressions and the XmlNamespaceManager, refer to the XmlNode.SelectNodes and XPathExpression.SetContext methods.
这篇关于为什么NamespaceManager没有XPath中不使用preFIX时使用DefaultNamespace的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!