Traversing an XML File

Eduardo Lorenzo

Well-known member
Joined
Jun 27, 2006
Messages
86
At the moment, I have a side-task of reading an XMLFile that looks like this:

<headerItem>
<text>Header 1</text>
<url>header1s url goes here</url>
<Table1>
<Text>HeaderItem 1</Text>
<url>headerItem 1s url goes here</url>
</Table1>
<Table1>
<Text>HeaderItem 2</Text>
<url>headerItem 2s url goes here</url>
</Table1>
</headerItem>

there is an unlimited/unpredictable number of headeritem nodes and Table1 nodes.

The task is to create one BulletedList (DisplayMode = Hyperlink) object per headeritem with headeritems <text> and <url> values as the Text and URL as the link. And then go through all the Table1 nodes and populate each BulletedList with the Text and url nodes/values per list.

this is what I have presently:

Code:
protected void xPathLoad()
{
		XmlDocument d = new XmlDocument();
		d.Load(Server.MapPath("/My_Page/App_Data/footer.xml"));
		foreach (XmlNode header in d.SelectNodes("//headerItem"))
		{
			BulletedList footerList = new BulletedList();
			footerList.DisplayMode = BulletedListDisplayMode.HyperLink;
			string headerText = header.SelectSingleNode("//text").InnerText + "header ";
			string headerURL = header.SelectSingleNode("//url").InnerText;
			footerList.Items.Add(new ListItem(headerText,headerURL));
			foreach (XmlNode items in header.SelectNodes("//Table1"))
			{
				string itemText = items.SelectSingleNode("//Text").InnerText;
				string itemURL = items.SelectSingleNode("//url").InnerText;
				footerList.Items.Add(new ListItem(itemText, itemURL));
			}
			  this.Controls.Add(footerList);
		}

but it selects only the first node??? :confused:

All help will be greatly appreciated. Thank you.
 
i see there are a couple things going on here...

First, XML is case sensitive. In your sample, you have two nodes of <Text> and one of <text>. so if you did ("//text") you would only get one node in your XmlNodeList.

Second, check out this quick ref for help on your xPath syntax.
specifically, you have errors with the double slashes "//"
those mean any node anywhere in the document.
when you want to select a first gen child to the current node, drop the slashes.


so, for example if you had:
Code:
<ROOT>
  <header>
    <table>
         <text />
         <url />
     </table>
  </header>
  <header>
    <table />
    <table />
    <table />
  </header>
</ROOT>
you would want
Code:
// this will return two nodes
// you can use ("//header") if you know all of your headers are direct children to your ROOT node. otherwise do this:
foreach (XmlNode header in d.SelectNodes("//ROOT/header"))   
{
   // first header node, this returns one table node
   // second header node, this returns three table nodes
   foreach (XmlNode table in header.SelectNodes("table")
   {
    string itemText = items.SelectSingleNode("text").InnerText;
    string itemUrl = items.SelectSingleNode("url").InnerText;
    }
}

and finally, because Im lazy and dont want to 2x post, check out the tools and tutorials here:
http://www.computerhelp.forum/showthread.php?t=100572

hope that helps!
 
what I have now is this:

Code:
 protected void readXML()
    {
        Panel myPanel = new Panel();
        XmlDocument d = new XmlDocument();
        d.Load(Server.MapPath("~/App_Data/footer.xml"));
        foreach (XmlNode header in d.SelectNodes("//headeritem"))
        {
            string headText = header.SelectSingleNode("text").InnerText; <---"object not set to an instance of an object"

            string headURL = header.SelectSingleNode("url").InnerText;
            foreach (XmlNode table in header.SelectNodes("Table1"))
            {
                string itemText = table.SelectSingleNode("Text").InnerText;
                string itemUrl = table.SelectSingleNode("url").InnerText;
            }
        }
    }

but the blasted thing wont go to the next Header node. And says "object not set to an instance of an object" when it gets to the next "headeritem" node.
could it be because of this:

Code:
<ROOT>
  <header>
        <text />      <---- I also have a text and url tag in the header.
         <url />
    <table>
         <text />
         <url />
     </table>
  </header>
  <header>
    <table />
    <table />
    <table />
  </header>
</ROOT>
 
Last edited by a moderator:
my Bad:

in the XML world.. "Text" is different from "text"

Thanks! The code posted in post #2 is now... alreadyused.. by me. :D
 

Similar threads

P
Replies
0
Views
118
Policy standard local admin account with Active Di
P
Back
Top