We help IT Professionals succeed at work.

Enumerating XML child nodes (2)

AlexFM
AlexFM asked
on
Medium Priority
1,093 Views
Last Modified: 2012-05-05
<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.
Comment
Watch Question

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) **/

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.