将命名空间添加到xpath

本文关键字:xpath 添加 命名空间 | 更新日期: 2023-09-27 17:59:03

我的类中充满了xpath,现在需要一个特殊的命名空间。我怎样才能做到这一点?重构这段代码最简单的方法是什么?找到替代品?

如果我不提前知道名称空间,那么将来获取特定节点的最佳方式是什么?

susr1 = root.SelectSingleNode("/PurchaseOrderRequest/DocumentHeader/DocumentInformation/DocumentIdentification/Type");
susr1 = root.SelectSingleNode("/PurchaseOrderRequest/ssdh:DocumentHeader/ssdh:DocumentInformation/ssdh:DocumentIdentification/ssdh:Type");

将命名空间添加到xpath

您可以尝试这个XML库。使用XPathElement()。如果你能使用的话,这是一个.Net 3.5解决方案。

XElement root = XElement.Load(file) or .Parse(string)

然后,只要不存在具有相同名称(但名称空间不同)的冲突节点,任何一个xpath都应该工作。

XElement susr1 = root.XPathElement("/PurchaseOrderRequest/DocumentHeader/DocumentInformation/DocumentIdentification/Type");
XElement susr1 = root.XPathElement("/PurchaseOrderRequest/ssdh:DocumentHeader/ssdh:DocumentInformation/ssdh:DocumentIdentification/ssdh:Type");

库应该为您找出名称空间。我不能在没有示例xml的情况下测试它。