getting all nodes and parent nodes in xml doc.

Goodday,

i have this code


                XmlDocument doc = new XmlDocument();
                doc.Load("C:\\Test.xml");

                XmlNode root = doc.SelectSingleNode("//top");

                foreach (XmlNode node in root.ChildNodes)
                {
                    Console.WriteLine(node.Name + ": " + node.InnerText);
                    Console.WriteLine(node.ParentNode);
                }


and running it against (example)

<main>
      <top>
            <naw>
                  <name>aa</name>
                  <adres>bb</adres>
            </naw>
            <algemeen>
                  <data>dat</data>
            </algemeen>
      </top>
</main>

what i need is that it just runs nicely over all nodes..like it would give me (in console or whatever)

naw - name - aa
naw - adres - bb
algemeen - data - dat

etc
what it does now  it gives me something like
naw - aabb
algemeen - dat

can someone supply me with an codeexample that does this, or fix the code i added here?
thank you very much!
kindest regards,
MArco

LVL 1
EnolaKotrotsosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Well, in its simplest form:

            XmlDocument doc = new XmlDocument();
            doc.Load("C:\\Test.xml");

            XmlNode top = doc.SelectSingleNode("//top");

            foreach (XmlNode node in top.ChildNodes)
            {
                foreach (XmlNode child in node.ChildNodes)
                {
                    Console.Write(node.Name + " - ");
                    Console.Write(child.Name + " - " + child.InnerText + "\n");
                }
            }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EnolaKotrotsosAuthor Commented:
there you go. thank you very much...
Marco
0
Bob LearnedCommented:
Marco,

Try this:

  private void NavigateDocument(string fileName, string startNode)
  {

    XmlDocument doc = new XmlDocument();
    doc.Load(fileName);

    XmlNode root = doc.SelectSingleNode("//" + startNode.TrimStart('/'));
    XmlNode child = null;

    if (root.HasChildNodes)
      child = root.ChildNodes[0];

    while (child != null)
    {
      this.DisplaySubNodes(child);
      child = child.NextSibling;
    }

  }

  private void DisplaySubNodes(XmlNode parent)
  {

    foreach (XmlNode child in parent.ChildNodes)
    {
      Console.Write(parent.Name + " - ");
      Console.WriteLine(child.Name + " - " + child.InnerText);
    }

  }

Example:
   this.NavigateDocument(@"C:\Temp\Test.xml", "top");

Bob
0
EnolaKotrotsosAuthor Commented:
hey bob, thanx..but you were really seconds late.
thanx though.
Marco
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

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.