I have the following XML node:
<reportDataRow>
<columnData colNum="1">
<data>FirstName</data>
</columnData>
<columnData colNum="2">
<data>LastName</data>
</columnData>
</reportDataRow>
I want to retrieve the value from the data
node based on the value of the colNum
attribute in the columnData
node.
How would I accomplish that using LINQ?
again assuming reportDataRow is an XElement and value is a variable that you want to match the colNum attribute with;
foreach (var selected in reportDataRow.Elements("columnData").Where(a =>a.Attribute("colNum").Value == value))
{
yield return selected.Element("data").Value;
}
usage will change based on how you want it
The easiest way is to use XPathSelectElement. Assuming that reportDataRow is an XElement representing the root element, and value is a variable that you want to match the colNum attribute with:
reportDataRow.XPathSelectElement(".//data[parent::columnData/@colNum = '" + value + "']");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.