Iterating Through All Nodes in Xml File

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.

Looping through all nodes in xml file with c#

I'm not in an environment to test code right now, but isn't it possible to write a recursive method to loop further down the nodes?

Something like this (untested code):

private static void handleNode(XmlNode node)
{
if(node.HasChildNodes)
{
foreach(XmlNode child in node.ChildNodes)
{
handleNode(child);
}
}
else
Console.WriteLine(node.Name);
}

then call this method in place of your Console.WrintLine()

How to iterate through all element in XML in Select Nodes

Try this if you want to log the inner text of all child nodes:

var customers = customersRaw.SelectNodes("tlp:WorkUnit", namespaceManager);
if (customers != null)
{
foreach (XmlNode customer in customers)
{
var childNodes = customer.SelectNodes("./*");
if(childNodes != null)
{
foreach(XmlNode childNode in childNodes)
{
if (Logger.IsDebugEnabled)
{
Logger.Debug(customerName.InnerText);
}
}
}
}
}

If you want to get the inner text of a specific node that you know the name of, you could use SelectSingleNode:

var childNode = customer.SelectSingleNode("./tlp:CustomerId", namespaceManager);
if(childNode != null)
Debug.WriteLine(childNode.InnerText);

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.

Iterate through XML nodes, add and remove some

I've found a solution. I have created two queues

NodesToRemove = new Queue<OpenXmlElement>();
NodesToInsert = new Queue<Tuple<OpenXmlElement, OpenXmlElement, InsertMode>>();

And after main document processing I iterate throug the queues and do add/remove operations

Iterate through xml file and store content inside Textfield c#

Using some LINQ-Magic you can do the folowing:

XElement root = XElement.Load("data.xml");
var subjects = from subject in root.Descendants()
where subject.Name.LocalName.Contains("Subject")
select new
{
SubjectName = subject.Element("subjectName").Value,
SubjectId = subject.Element("subjectId").Value,
SubjectValue = subject.Element("subjectvalue").Value
};

foreach (var subject in subjects)
{
Console.WriteLine(subject);

//you can use subject like this:
string subjectName = subject.SubjectName;
string subjectId = subject.SubjectId;
string subjectValue = subject.SubjectValue;
}

This will print:

{ SubjectName = maths, SubjectId = qq1, SubjectValue = 20 }
{ SubjectName = science, SubjectId = sla1s, SubjectValue = 25 }

Includes:

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;

iterate through all nodes of an xml file

Your code doesn't go into <TelephoneNumbers> because of line

If (node.ChildNodes.Count > 1) Then

There's only one child element here so it stops at that level.

Iterate through nested XML nodes and save it to a list in asp.net c#

I prefer @Yitzhak solution, but if you want to use XmlDocument, you could try the following :

1 - Create class ActionObject:

public class ActionObject
{
public string Name { get; set; }

public string Type { get; set; }

public string Variable { get; set; }
}

2 - Xml

string xml2 = @"
<xml>
<record>
</record>
<Actions>
<Action Name= 'name1'>
<screen></screen>
<subAction></subAction>
</Action>
<Action Name = 'name2'>
<screen Description= 'info'>
<subAction>
<object>
<name> user </name>
<type> string </type>
<variable> ram </variable>
</object>
<object>
<name> user1 </name>
<type> string1 </type>
<variable> ram1 </variable>
</object>
</subAction>
</screen>
<Screen Description= 'info1'>
<subAction>
<object></object>
</subAction>
</Screen>
</Action>
</Actions>
</xml>";

3 - Code that get objects from xml:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml2);
List<ActionObject> objects = new List<ActionObject>();

XmlNodeList actions = xmlDocument.DocumentElement.SelectNodes("/xml/Actions/Action");
foreach (XmlElement actionNode in actions)
{
if (actionNode.Attributes["Name"].Value != "name2")
continue;

foreach(XmlNode objectNode in actionNode.SelectNodes("./screen/subAction/object"))
{
ActionObject actionObject = new ActionObject
{
Name = objectNode.SelectSingleNode("name").InnerText.Trim(),
Type = objectNode.SelectSingleNode("type").InnerText.Trim(),
Variable = objectNode.SelectSingleNode("variable").InnerText.Trim(),
};

objects.Add(actionObject);
}
}

I hope you find this helpful.



Related Topics



Leave a reply



Submit