How to show value of attributes of child nodes of an element in C#?
How to show value of attributes of child nodes of an element in C#?
Lets say you have following XML and you have to fetch IDs of Element tags which are under Segments/Segment/Elements. In this case you have to use SelectNodes method to reach upto desired nodes and then take it to the XMLNodeList. Iterate the list check its child nodes and get the attributes of child nodes. I think below code can clear what I am trying to say.
My XML File
My C# code
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(MyXMLFileLocation);
xmlDocument.Load(MyXMLFileLocation);
XmlNodeList xnList = xmlDocument.SelectNodes(/Segments/Segment[@ID='AAA']/Elements);
foreach (XmlNode xn in xnList)
{
if (xn.HasChildNodes)
{
foreach (XmlNode childNode in xn.ChildNodes)
{
Console.Writeline(childNode.Attributes["ID"].Value);
}
}
}
foreach (XmlNode xn in xnList)
{
if (xn.HasChildNodes)
{
foreach (XmlNode childNode in xn.ChildNodes)
{
Console.Writeline(childNode.Attributes["ID"].Value);
}
}
}