kag Posted December 3, 2009 Share Posted December 3, 2009 I have the following code and I cannot figure why xnList is always an empty list. I'm not sure if "xml" doesn't contain what I think it does, of if my XPath expression is wrong. This is the XML file I'm fetching: http://picasaweb.google.com/data/feed/api/...bert?kind=album string url = "http://picasaweb.google.com/data/feed/api/user/" + username.Text + "?kind=album"; WebRequest request = WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); response.Close(); dataStream.Close(); reader.Close(); XmlDocument xml = new XmlDocument(); xml.LoadXml(responseFromServer); XmlNodeList xnList = xml.SelectNodes("/feed/entry"); MessageBox.Show("Found: " + Convert.ToString( xnList.Count )); Link to comment Share on other sites More sharing options...
0 Eric Veteran Posted December 3, 2009 Veteran Share Posted December 3, 2009 I'm not sure how Google's namespaces are set up, but they have a .NET API library that's available that can handle all the requests for you: http://code.google.com/apis/picasaweb/docs...ide_dotnet.html I'm sure you could look through it and figure out what's missing. I suspect it's a namespace manager issue. Link to comment Share on other sites More sharing options...
0 kag Posted December 3, 2009 Author Share Posted December 3, 2009 You're absolutely right. Someone else on another forum just answered pretty much the same thing. Here's what fixed my problem, although I'm not sure I understand the whole XML namespace thing. I added these two lines and ajusted my XPath expression. XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("fd", "http://www.w3.org/2005/Atom"); XmlNodeList xnList = xml.SelectNodes("/fd:feed/fd:entry", nsmgr); Link to comment Share on other sites More sharing options...
0 garethevans1986 Posted December 3, 2009 Share Posted December 3, 2009 Using XElement/XDocument (Linq to XML) is also another option for you rather than using XmlNodeList and looping. GE Link to comment Share on other sites More sharing options...
0 Eric Veteran Posted December 3, 2009 Veteran Share Posted December 3, 2009 You're absolutely right. Someone else on another forum just answered pretty much the same thing. Here's what fixed my problem, although I'm not sure I understand the whole XML namespace thing. I added these two lines and ajusted my XPath expression. XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable); nsmgr.AddNamespace("fd", "http://www.w3.org/2005/Atom"); XmlNodeList xnList = xml.SelectNodes("/fd:feed/fd:entry", nsmgr); I saw that in the MSDN library. That was to be my next suggestion if you weren't interested in Google's .NET library. :) I just wasn't sure which namespace you were trying to access. Link to comment Share on other sites More sharing options...
Question
kag
I have the following code and I cannot figure why xnList is always an empty list. I'm not sure if "xml" doesn't contain what I think it does, of if my XPath expression is wrong.
This is the XML file I'm fetching:
http://picasaweb.google.com/data/feed/api/...bert?kind=album
string url = "http://picasaweb.google.com/data/feed/api/user/" + username.Text + "?kind=album"; WebRequest request = WebRequest.Create(url); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); response.Close(); dataStream.Close(); reader.Close(); XmlDocument xml = new XmlDocument(); xml.LoadXml(responseFromServer); XmlNodeList xnList = xml.SelectNodes("/feed/entry"); MessageBox.Show("Found: " + Convert.ToString( xnList.Count ));Link to comment
Share on other sites
4 answers to this question
Recommended Posts