Hi Guys,
I'm having some trouble selecting the some XML nodes from a Flickr
atom feed. He's the a cut down version of the code to illustrate my
problem:
using System.Xml;
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
HttpWebRequest webRequest =
(HttpWebRequest)WebRequest.Create("http://api.flickr.com/services/
feeds/photos_public.gne");
webRequest.Timeout = 10000;
// Makes sure the connection is not persistant
webRequest.KeepAlive = false;
HttpWebResponse webResponse = null;
webResponse = (HttpWebResponse)webRequest.GetResponse();
xmlDoc.Load(webResponse.GetResponseStream());
XmlNamespaceManager NsMgr = new
XmlNamespaceManager(xmlDoc.NameTable);
NsMgr.AddNamespace("", "http://www.w3.org/2005/Atom");
NsMgr.AddNamespace("dc", "http://purl.org/dc/elements/1.1/");
XmlElement root = xmlDoc.DocumentElement;
// XmlNodeList nodeList1 = root.
XmlNodeList nodeList = root.SelectNodes("/feed/entry", NsMgr);
Response.Write("The number of nodes is:" + nodeList.Count);
}
When debugging the atom feed is loaded into the 'root' XmlElement. But
then I can't seem to select the nodes with my Xpath query.
Thanks for any assistance.
Alun
Martin Honnen - 17 Mar 2008 14:51 GMT
> xmlDoc.Load(webResponse.GetResponseStream());
>
> XmlNamespaceManager NsMgr = new
> XmlNamespaceManager(xmlDoc.NameTable);
>
> NsMgr.AddNamespace("", "http://www.w3.org/2005/Atom");
You need to bind a prefix to the default namespace e.g
NsMgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
then use that prefix in your XPath expressions e.g.
XmlNodeList nodeList = xmlDoc.SelectNodes("/atom:feed/atom:entry",
NsMgr);

Signature
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/