Programming in C# I got an Xml.XpathNodeList object “ResultsPartRel.nodeList”. Debugging it with Visual Studio I can read “Results View ; Expanding the Results View will enumerate the IEnumerable”
Questions:
1.- Which is the best way to read those nodes?
2.- I program the next code but I dont get the expected results. I get the same result twice. (ResultsPartRel.nodeList contains 2 nodes)
List<string> childrenName = new List<string>();
foreach (XmlElement node in ResultsPartRel.nodeList)
{
string nameChildren = node.SelectSingleNode("//related_id/Item/keyed_name").InnerText;
childrenName.Add(nameChildren);
}
Thank you in advance.
EDIT
<related_id>
<Item>
<classification>Component</classification>
<id></id>
<keyed_name>glass</keyed_name> <!-- I want to get this InnerText -->
</Item>
</related_id>
<source_id>968C45A47942454DA9B34245A9F72A8C</source_id>
<itemtype>5E9C5A12CC58413A8670CF4003C57848</itemtype>
Well we really need to see the XML sample and a verbal explanation of which data you want to extract. Currently you do a
node.SelectSingleNode(...)so that looks as if you want to select a path relative tonodebut then you use an absolute path starting with//, that is why you get the same result twice.So you want
node.SelectSingleNode(".//related_id/Item/keyed_name")or perhaps evennode.SelectSingleNode("related_id/Item/keyed_name"), depending on the XML you have.