我正在尝试提出正确的 XPath,以便根据不属于要使用 C# 中的 XDocument.XPathSelectElement
方法选择的 XPath 的条件来获取节点。
假设我有以下 XML。
<Root>
<Parties>
<Party>
<Person>
<Name>Bob Smith</Name>
</Person>
<Role>
<Type>Borrower</Type>
</Role>
</Party>
<Party>
<Person>
<Name>Mary Smith</Name>
</Person>
<Role>
<Type>CoBorrower</Type>
</Role>
</Party>
</Parties>
</Root>
所以我想要得到的是
的值//Root/Parties/Party/Person/Name
哪里
//Root/Parties/Party/Role/Type
是“借款人”。
根据我在发布问题之前的研究,我认为以下方法可能有效,但没有运气。
XDocument xDoc = XDocument.Parse(xml);
var elm = xDoc.XPathSelectElement("//Root/Parties/Party[./Role/[Type = 'Borrower']/Person/Name");
但是这段代码给了我以下异常:
An unhandled exception of type 'System.Xml.XPath.XPathException' occurred in System.Xml.dll
Additional information: Expression must evaluate to anode-set
.
最佳答案
使用此 XPath 表达式:
//Root/Parties/Party[Role/Type/text() = 'Borrower']/Person/Name
它会给你想要的结果
Bob Smith
关于c# - 具有不同节点路径的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42494600/