• 0

C# - XML / XPath problem


Question

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

  • 0

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

  • 0

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

  • 0
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

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.