I have the following XML:
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="true">
<entity name="contact">
<attribute name="contactid" />
<link-entity name="ccl1007_studentjourney" from="ccl1007_contactid" to="contactid">
<filter type="and">
<condition attribute="statecode" operator="eq" value="0" />
<filter type="or">
<condition attribute="ccl1007_sjapplicantdayattendedon" operator="last-x-months" value="12" />
<condition attribute="ccl1007_sjapplicantdayattendedon" operator="next-x-years" value="5" />
</filter>
</filter>
</link-entity>
<filter>
<condition attribute="contactid" operator="eq" value="234" />
</filter>
</entity>
</fetch>
What I want to do is get all <condition/> nodes inside the <link-entity/> node and ignore other instances of the <condition/>.
Using my example, I want the output to only return the following nodes
<condition attribute="statecode" operator="eq" value="0" />
<condition attribute="ccl1007_sjapplicantdayattendedon" operator="last-x-months" value="12" />
<condition attribute="ccl1007_sjapplicantdayattendedon" operator="next-x-years" value="5" />
I tried the following but it was only able to loop on the first <filter/> tag that contains the first <condition/> tag
XmlNode linkEntityElem = doc.GetElementsByTagName("link-entity")[0];
foreach (XmlNode child in linkEntityElem.SelectNodes("filter"))
{
var g = child.Attributes["attribute"];
}
All you need is two xpath queries:
SelectSingleNode's parameter will find the<lint-entity>nodeSelectNodes's parameter will find all<condition>nodes under the<link-entity>node regardless of the depthSelectNodesis anXmlNodeListso you have to explicitly useXmlElementinside the foreach to be able to useOuterXmlThe output will be
Here you can find the working demo on sharplab.