Enumerating XML child nodes (2)

<List>
  <Employees>
      <Name>Peter</Name>
      <Name>John</Name>
  </Employees>
  <Managers>
      <Name>Bill</Name>
  </Managers>
</List>

I need to enumerate this XML using XMLDocuments class, but without using GetElementsByTagName method, because tag names are not unique. I need the way to enumerate XMLNode childs by name.
LVL 48
AlexFMAsked:
Who is Participating?
 
Jase-CoderCommented:
try something like:

        /** Description:
         ** This function recursivly traverses a list of
         ** Xml nodes. Used for testing only.
         **
         ** Author:
         ** Jason Myerscough
         **/
        void Traverse(Int32 Index, XmlNodeList Nodes)
        {
            XmlNode CurrentNode = Nodes.Item(Index);

            if (CurrentNode != null)
            {
                if (CurrentNode.HasChildNodes == true)
                    Traverse(0, CurrentNode.ChildNodes);        // if there is childnodes traverse them

                Traverse(Index + 1, Nodes);           // traverse the siblings
                if (CurrentNode.HasChildNodes == false)
                    Console.WriteLine(CurrentNode.Name + " = " + CurrentNode.InnerText);
            }
        }   /** void Traverse(Int32 Index, XmlNodeList Nodes) **/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.