【发布时间】:2018-12-21 15:38:04
【问题描述】:
我有以下 XML 结构,其中包含很多节点 <PName>。我需要做的是运行 Xpath 查询匹配条件提取一些数据。运行到下一个节点测试条件,然后返回 XPath 查询并继续该过程
这是我的 XML:
<?xml version="1.0"?>
<PatientDetailsXML>
<PList>
<PName type="Patient">
<properties>
<Room bedType="Auto"/>
<PName title="Joe Beom" PId="1234">
<Details>
<classification classification="paymenttype" category="Wallet"/>
<classification classification="Humor" category="None"/>
<classification classification="Food" category="Fruit"/>
</Details>
</PName>
</properties>
<childEvents>
</childEvents>
</PName>
<PName type="Patient">
<properties>
<Room bedType="Auto"/>
<PName title="John Bair" PId="1234">
<Details>
<classification classification="paymenttype" category="Found"/>
<classification classification="Humor" category="None"/>
<classification classification="Food" category="Fruit"/>
</Details>
</PName>
</properties>
<childEvents>
</childEvents>
</PName>
</PList>
</PatientDetailsXML>
这是我的代码:
var query = @"//PName[.//PName[Details/classification[@classification='paymenttype' and @category='Wallet']]]";
foreach (XmlNode n in docs.SelectNodes(query))
{
var titlelink = n.SelectSingleNode(".//PName/@title");
var title = titlelink.Value;
var bedlink = n.SelectSingleNode(".//Room/@bedType");
var bed = bedlink.Value;
// Here I want to run to the very next node <PName> and do
// some test's such as `classification='paymenttype' and
// @category='Wallet'`, if not true insert some data in XML
// jump back to the XPATH node (where query was working at
// and continue the iteration).
// If it matches I fetch some data.
}
我真的不知道如何在没有条件的情况下像这样强制导航,我们将不胜感激。
【问题讨论】:
-
这是一个令人困惑的问题。你的标题说没有条件。你的第一段说它必须满足条件,你以“没有条件”结束。在我看来,如果没有任何类型的分支代码(条件),编写任何代码都非常困难。