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
C# ASP.NET MVC Download a File Via Ajax
Rendering Views from External Assemblies in Mvc6
How to Open the Default Chrome Profile Through Selenium, Chromedriver and Googlechrome
File and Line Numbers for Errors Are Not Displaying in the Error List Unless the File Is Open
Why Does Integer Division in C# Return an Integer and Not a Float
Storing Enums as Strings in Mongodb
Error 5:Access Denied When Starting Windows Service
Programmatically Close Aspx Page from Code Behind
How to Add Double Quotes to a String That Is Inside a Variable
How to Loop Through All Fields in an Object in C#
How to Convert a Dictionary to a Json String in C#
Drawing a Triangle With Rounded Corners
Filtering Out Values from a C# Generic Dictionary
Generate Xml for <Govtalkmessage Xmlns="Http://Www.Govtalk.Gov.Uk/Cm/Envelope">
How to Call Another Controller Action from a Controller in MVC