Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that
and that didn't work either.
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.
ska_mna
Member
10 Points
39 Posts
Re: Loop through sub XmlNodeList with C#
Apr 12, 2007 08:17 PM|LINK
Thanks very much for the reply. I've tried your suggested code and it does seem to loop though now, so that's great. However, it doesn't write out the value for categoryName. My code now is:
XmlTextReader xtr = new XmlTextReader("http://url.feed.com"); xtr.WhitespaceHandling = WhitespaceHandling.None; XmlDocument X = new XmlDocument(); X.Load(xtr); if (!(X == null)) { try { XmlNodeList ArticleList = X.SelectNodes("InfoStreamResults/Article"); foreach(XmlNode Article in ArticleList) { try { XmlNodeList CatNodesList = Article.SelectNodes("Categories/Category"); foreach( XmlNode category in CatNodesList) { if (Article.SelectSingleNode("Category") != null) { categoryName = EncodeIt(Article.SelectSingleNode("Category").Value); Response.Write(categoryName); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); Response.Write("<br />"); } } } catch (Exception ex) { Response.Write(ex); } }In the above, we don't enter the if statement on line 22. Is this the right way of referencing the node? I would have thought that now we are in another foreach loop, it would become category.SelectSingleNode("category"). But I tried that and that didn't work either.
Incidentally, a sample of my XML looks like this;
You can see the Categories node as above and the two values inside it. How do you loop through each article, then loop through each set of Categories inside each Article, and get the value of each Category? Getting there - any more pointers much appreciated.