View Single Post
  #2 (permalink)  
Old December 18th, 2007, 05:15 AM
samjudson's Avatar
samjudson samjudson is offline
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

To loop through a selection of nodes in C# you can do the following:

foreach(XmlNode applicationNode in document.SelectNodes("//Application"))
{
   // Do something
}

You can do a similar thing for each element in your array:

foreach(string nodePattern in nodeList)
{
  foreach(XmlNode node in applicationNode.SelectNodes(nodePattern))
  {
     // Do something else
  }
}

At this point I would pass the XmlNode 'node' to a recursive function that prints out all attributes and then passes each child node to itself.

e.g.:

public static void PrintNode(XmlNode node)
{
  foreach(XmlNode attribute in node.Attributes)
  {
    Console.WriteLine(attribute.Name + " = " + attribute.Value);
  }
  foreach(XmlNode child in node.ChildNodes)
  {
    // print out name
    Console.Write(child.Name);
    // pass to recursive function.
    PrintNode(child);
  }
}


/- Sam Judson : Wrox Technical Editor -/
Reply With Quote