Eduardo Lorenzo Posted July 2, 2007 Posted July 2, 2007 At the moment, I have a side-task of reading an XMLFile that looks like this: <headerItem> <text>Header 1</text> <url>header1's url goes here</url> <Table1> <Text>HeaderItem 1</Text> <url>headerItem 1's url goes here</url> </Table1> <Table1> <Text>HeaderItem 2</Text> <url>headerItem 2's 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 headeritem's <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: 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. Quote
alreadyused Posted July 3, 2007 Posted July 3, 2007 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. http://www.w3schools.com/xpath/xpath_syntax.asp 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: <ROOT> <header> <table> <text /> <url /> </table> </header> <header> <table /> <table /> <table /> </header> </ROOT> you would want // 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 I'm lazy and don't want to 2x post, check out the tools and tutorials here: http://www.xtremedotnettalk.com/showthread.php?t=100572 hope that helps! Quote
Eduardo Lorenzo Posted July 16, 2007 Author Posted July 16, 2007 (edited) what I have now is this: 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 won't 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: <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> Edited July 16, 2007 by Eduardo Lorenzo Quote
Eduardo Lorenzo Posted July 16, 2007 Author Posted July 16, 2007 my Bad: in the XML world.. "Text" is different from "text" Thanks! The code posted in post #2 is now... alreadyused.. by me. :D Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.