Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 297
  • Last Modified:

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

0
EnolaKotrotsos
Asked:
EnolaKotrotsos
  • 2
1 Solution
 
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
 
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now