在下面的虚拟XML文件中,我必须在stepContent节点中找到搜索字符串并返回ElementID和stepID。我在C#中使用Linq,而我能够找到搜索字符串并返回整个节点,我无法弄清楚如何返回搜索节点的stepID和ElementID。请注意这是虚拟XML,这些ID节点的深度可能会有所不同,所以我需要一些东西来查询返回值中的名称。
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Elements>
<Element>
<ElementID>A001</ElementID>
<Detail><![CDATA[<ul>
<li>
For top candidates
</li>
<li>
Discount upto 50%
</li>
</ul>]]></Detail>
<Steps>
<Step>
<stepID>S001</stepID>
<StepHeading>Prepare for top candidates</StepHeading>
<stepContent><![CDATA[<ul>
<li>Some dummy text</li>
<li>Plan some dummy items.</li>
</ul>]]></stepContent>
</Step>
<Step>
<stepID>S002</stepID>
<StepHeading>Invite top candidates</StepHeading>
<stepContent><![CDATA[<ul>
<li>Dummy text for invitation.</li>
<li>Dummy text for 2nd invitation.</li>
</ul>]]></stepContent>
</Step>
</Steps>
</Element>
<Element>
<ElementID>A002</ElementID>
<Detail><![CDATA[<ul>
<li>
For next set of top candidates
</li>
<li>
Discount upto 30%
</li>
</ul>]]></Detail>
<Steps>
<Step>
<stepID>S003</stepID>
<StepHeading>Prepare for next set of top candidates</StepHeading>
<stepContent><![CDATA[<ul>
<li>Some dummy text</li>
<li>Plan some dummy items.</li>
</ul>]]></stepContent>
</Step>
<Step>
<stepID>S004</stepID>
<StepHeading>Invite next set of top candidates</StepHeading>
<stepContent><![CDATA[<ul>
<li>Dummy text for invitation.</li>
<li>Dummy text for 2nd invitation.</li>
</ul>]]></stepContent>
</Step>
</Steps>
</Element>
</Elements>
</Root>
答案 0 :(得分:0)
“我必须在
stepContent
节点中找到搜索字符串并返回ElementID
和stepID
”
只要来自ElementID
的{{1}}和stepID
的相对深度一致,您就可以这样做:
stepContent
答案 1 :(得分:0)
你可以这样做。
var doc = XDocument.Load(filepath);
var steps = doc.Root.Descendants("Step")
.Where(e=> ((string)e.Element("stepContent").Value).ToLower().Contains("dummy text for "))
.Select(s=> new
{
StepId = s.Element("stepID").Value,
StepHeading = s.Element("stepID").Value
});
选中此Demo