解析 XML:我的元素中的冒号导致 XPath 错过它

发布于 2024-08-08 05:01:52 字数 702 浏览 1 评论 0原文

我加载了一个 XML 文档并尝试使用 XPath 进行搜索。该文件中的根节点是 并且文件末尾使用 正确关闭代码>.

我的问题是,如果不使用后代轴,我就无法遍历树。换句话说,我可以执行以下操作: SelectSingleNode("//TransactionHeader[SHIPPERSTATE='CA']") 并获得一个节点作为回报。但我无法执行等效操作: SelectSingleNode("/Transmission/TransmissionBody/Transaction/TransactionHeader[SHIPPERSTATE='CA']")

如果我删除 t:我可以对 /Transmission 进行 XPath 搜索并获取整个文件。有了 t: 在那里我就得到了 null。或者,如果我尝试 SelectSingleNode("t:Transmission"),我的 XPath 语句会出现错误。

我通常不需要查询根元素,因此我应该能够仅使用后代轴进行搜索。但 XML 对我来说看起来有效,所以我想知道如何解决这个问题。另外,我不想要求客户删除“t:”只是因为我不知道如何处理它。

I have an XML document that I load in and try to search with XPath. The root node in this file is <t:Transmission xmlns:t='urn:InboundShipment'> and the file end is properly closed with </t:Transmission>.

My problem is that I cannot walk the tree without using a descendant axis. In other words, I can do: SelectSingleNode("//TransactionHeader[SHIPPERSTATE='CA']") and get a node in return. But I cannot do what should be the equivalent: SelectSingleNode("/Transmission/TransmissionBody/Transaction/TransactionHeader[SHIPPERSTATE='CA']")

If I remove the t: I can do an XPath search on /Transmission and get the whole file. With the t: in there I just get null. Or if I try SelectSingleNode("t:Transmission") I get an error with my XPath statement.

I generally do not need to query the root element, so I should be able to make do with just using the descendant axis for my searches. But the XML looks valid to me and so I'd like to know how to address this. Plus I don't want to ask the client to remove "t:" just because I don't know how to deal with it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无悔心 2024-08-15 05:01:52

“t:”是命名空间前缀,它绑定到命名空间“urn:InboundShipment”。为了正确处理它,您必须告诉 c# 前缀绑定到什么。 此页应说明如何使用 System.Xml.XmlNamespaceManager 处理命名空间。

编辑:另请参阅此答案

The "t:" is a namespace prefix, which is bound to the namespace 'urn:InboundShipment.' In order to properly handle it, you have to tell c# what the prefix is bound to. This page should explain how to use System.Xml.XmlNamespaceManager to handle the namespace.

Edit: See this answer, as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文