Im having trouble getting a XmlNodeList where there is 1 child with a specific name that
<tables>
<table tableName="Orders">
<item table="Orders">
...
</item>
<item table="Orders">
...
</item>
</table>
<table tableName="OrderWithParent">
<item table="OrderWithParent">
<column columnName="OrderWithParentId"><![CDATA[156]]></column>
<column columnName="OrderParentId"><![CDATA[1]]></column>
...
</item>
<item table="OrderWithParent">
<column columnName="OrderWithParentId"><![CDATA[156]]></column>
<column columnName="OrderParentId"><![CDATA[1]]></column>
...
</item>
<item table="OrderWithParent">
<column columnName="OrderWithParentId"><![CDATA[156]]></column>
<column columnName="OrderParentId"><![CDATA[2]]></column>
...
</item>
</table>
</tables>
So thats my basic xml layout...
Ive deserialized the top Orders... and now i wanna find the OrderWithParent's where their column with columnName="OrderParentId" == order.Id
the order nodes where retrieved like this:
var orders = root.SelectNodes("/tables/table[@tableName='Orders']/item[@table='Orders']");
So atm im using an XmlDocument.. im hope to using XDocument aswell.. but i havent been able to find a solution with either of them. Help is much appreciated!
Try this
Assuming that
order.Id
meansId
property of a predefined variableorder
which you didn't show, the following XPath should return the targetitem
element :For older C# version where string interpolation
$
is not supported :Another assumption is, that
order.Id
value is always number. Otherwise you'll need to wrap the value with quotes in the XPath i.e in the above snippet, replace{order.Id}
with'{order.Id}'
or{0}
with'{0}'
.If you switch to using
XDocument
, you can still execute the same XPath expression throughXPathSelectElements()
,XPathSelectElement()
, orXPathEvaluate()
methods.I think your searching for the wrong node in your syntax. Try this, see if it helps:
this assumes I'm not misunderstanding the question though.