How to Iterate Though Each Child Node in an Xml File

How can I iterate though each child node in an XML file?

You're iterating the FieldData nodes and you have only one. To iterate its child nodes write:

foreach (XmlNode node in dataNodes)
{
foreach (XmlNode childNode in node.ChildNodes)
{

Iterate over child nodes in an XML document

You want to do..

foreach (XmlNode n in doc.SelectSingleNode("/configuration/Points").ChildNodes)
{
MessageBox.Show(n.Name);
}

Your xpath query does only select the node "Points", but you want to iterate its child nodes.

Reading xml child nodes in foreach loop

As mentioned in the comment, you need to get URL of the XML where the actual temperature resides, as well as the corresponding XPath to locate the temperatures within the XML. Load XML from the URL into XmlDocument and then execute the XPath to extract the actual temperature values :

foreach (XmlNode node in xml)
{
// get information needed to extract temprature i.e XML location, and the XPath :
var loc = node.SelectSingleNode("url").InnerText;
var xpath = node.SelectSingleNode("xpath").InnerText;

// get temperature information
var doc = new XmlDocument();
doc.Load(loc);
var temperatures = doc.SelectNodes(xpath);

// iterate through temperatures and take action accordingly, f.e print the temperature
foreach(XmlNode temperature in temperatures)
{
Console.WriteLine(temperature.Attributes["value"].Value);
}
}

XML Element Tree Python Iterate through child and save each subchild as CSV column

Simply add nested for loops to parse the LINK and TAG children.

for contact in root.findall('Personnel'):
...
for link in contact.findall('.//LINK'):
contact_info.append(link.get('COMPANY'))
contact_info.append(link.get('ROLE'))

for tag in contact.findall('.//TAG'):
contact_info.append(tag.get('Term'))

Loop through all nodes in xml and save path & value of nodes

Not sure what you mean with grand child nodes: are those the child nodes in the document element?

I think what you are trying todo is iterate through all XmlNodes and log and for each XmlNode also check for it's ChildNodes and do the same for each ChildNode:

ProcessNodes(doc.DocumentElement.ChildNodes);

private void ProcessNodes(XmlNodeList xmlNodes) {
foreach (XmlNode xmlNode in xmlNodes) {
//TODO: Do something here with xmlNode, like logging Name and value

//Now call ProcessNodes for it's ChildNodes
ProcessNodes(xmlNode.ChildNodes);
}
}

If you also want to log the DocumentElement you can also change the inputParameter to an XmlNode. The concept of iteration stays the same:

ProcessNode(doc.DocumentElement);

private void ProcessNode(XmlNode xmlNode) {
//TODO: Do something here with xmlNode, like logging Name and value

foreach (XmlNode childNode in xmlNode.ChildNodes) {
//Now call ProcessNode for it's Child
ProcessNode(childNode);
}
}

Iterating through all nodes in XML file

I think the fastest and simplest way would be to use an XmlReader, this will not require any recursion and minimal memory foot print.

Here is a simple example, for compactness I just used a simple string of course you can use a stream from a file etc.

  string xml = @"
<parent>
<child>
<nested />
</child>
<child>
<other>
</other>
</child>
</parent>
";

XmlReader rdr = XmlReader.Create(new System.IO.StringReader(xml));
while (rdr.Read())
{
if (rdr.NodeType == XmlNodeType.Element)
{
Console.WriteLine(rdr.LocalName);
}
}

The result of the above will be

parent
child
nested
child
other

A list of all the elements in the XML document.



Related Topics



Leave a reply



Submit