How can I traverse (read all the nodes in order) a XML document using recursive functions in c#?
What I want is to read all the nodes in xml (which has attributes) and print them in the same structure as xml (but without Node Localname)
Thanks
How can I traverse (read all the nodes in order) a XML document using recursive functions in c#?
What I want is to read all the nodes in xml (which has attributes) and print them in the same structure as xml (but without Node Localname)
Thanks
IEnumerable<atype> yourfunction(XElement element)
{
foreach(var node in element.Nodes)
{
yield return yourfunction(node);
}
//do some stuff
}
using System.Xml;
namespace ConsoleApplication1
{
class Program
{
static void Main( string[] args )
{
var doc = new XmlDocument();
// Load xml document.
TraverseNodes( doc.ChildNodes );
}
private static void TraverseNodes( XmlNodeList nodes )
{
foreach( XmlNode node in nodes )
{
// Do something with the node.
TraverseNodes( node.ChildNodes );
}
}
}
}