3 Replies - 3519 Views - Last Post: 01 August 2011 - 10:28 AM Rate Topic: -----

#1 tehwabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-November 10

Handling nested XML in C# using XmlDocument

Posted 01 August 2011 - 07:24 AM

When I need to read from XML I usually use something like the following:


XmlDocument doc = new XmlDocument();
doc.load(location);
string query = "/item/subitem";
XmlNodeList nodeList = doc.SelectNodes(query);

foreach (XmlNode item in nodeList){

...do stuff...

}



However, this only works for one deep XML structures like:

<menu>
<submenu>
<name>Hello world<name>
<name>Hello world<name>
<name>Hello world<name>
<name>Hello world<name>
</submenu>
</menu>


What if I had another level to delve into?

i.e:

<menu>
<submenu>
<name>Hello world<name>
<anothermenu>

<some other attributes>

</anothermenu>
<name>Hello world<name>
<name>Hello world<name>
<name>Hello world<name>
</submenu>
</menu>


I have tried delving into child nodes with no luck? Can anybody point me in the right direction?

Is This A Good Question/Topic? 0
  • +

Replies To: Handling nested XML in C# using XmlDocument

#2 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Handling nested XML in C# using XmlDocument

Posted 01 August 2011 - 08:02 AM

It depends on what you're trying to extract. You're not being very specific here. Give us a real example instead of made up, invalid XML so we can see what you're actually trying to do.
Was This Post Helpful? 0
  • +
  • -

#3 tehwabbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-November 10

Re: Handling nested XML in C# using XmlDocument

Posted 01 August 2011 - 08:13 AM

<menu>
 <menu_items>
     <anchorName> Home </anchorname>
 </menu_items>   
  <menu_items>
     <anchorName> Contact</anchorname>
     <sub_menu_items>
        <menu_items>
            <anchorName> Email</anchorname>
         </menu_items>  
        <menu_items>
            <anchorName> Chat Live</anchorname>
         </menu_items>  
     <sub_menu_items>
 </menu_items> 



That is an exact piece of XML, didn't have it to hand when I first posted. Basically I want to be able to access the contents of <sub_menu_items> WHEN it exists. I've only ever used LINQ to solve issues like this before (Can't use LINQ in this project though due to certain constraints)

The current code will allow me to get the name(anchor name) of each node in menu_items


---------------Output example-----------------

Home
COntact
-email
-live chat

This post has been edited by tehwabbit: 01 August 2011 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,671
  • Joined: 23-August 08

Re: Handling nested XML in C# using XmlDocument

Posted 01 August 2011 - 10:28 AM

This looks like a good use case for XML Serialization.

Tutorial
Snippet
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1