ASP XML parsing selectsinglenode returns null node

Hello, I'm using ASP.NET 3.5 and I'm trying to parse the XML response from the contacts list. Here's my code:

Uri address = new Uri(CCURL_Contacts + "?email=xxxxx%40xxxxxx.com");
HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
request.Credentials = new NetworkCredential(CCAPIKey + "%" + CCLoginName, CCLoginPassword);
request.Method = "GET";
request.ContentType = "application/atom+xml";

//Get the data as an HttpWebResponse object
HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();

//Convert the data into a string
StreamReader reader = new StreamReader(HttpWResp.GetResponseStream());
String results = reader.ReadToEnd();
reader.Close();

// Handle the XML data
XmlDocument doc = new XmlDocument();
doc.LoadXml(results);

XmlNode root = doc.DocumentElement;
XmlNode detail;

// ******* No matter what I put for the Xpath string parameter, the following always results in detail = null
detail = root.SelectSingleNode("/entry/id");
**********************************

// The following works but is limiting.
XmlNodeList nodes = doc.GetElementsByTagName("id");
detail = nodes.Item(1);

I can't seem to get SelectSingleNode to work, although it would tremendously facilitate further coding. I've read about namespaces but I don't understand it enough to know if that's where the issue is and I can't figure out how to use namespaces in conjunction with the response XML data and Xpaths. And maybe that's the wrong tree to bark up at.

Can anyone offer any solution so that I may use SelectSingleNode? I've looked all around the forums and documentation and I can't find much there.

Thanks in advance.

re: ASP XML Parsing

I haven't used SelectSingleNode yet for parsing the XML Document, I have been using XMLStreamReader instead to have it parse the XML nodes for me and combine that with a switch statement to populate a class depending on what I am GETing. However, Constant Contact does not set up a namespace in any of the XML returned from GET statements. I don't believe that is the problem you are running into.

I've been researching XPath search strings, it appears that you're looking trying to search from the current position on the document for the entry node followed by the id node. The problem seems to be in the form of the XPath itself. Have you tried using an absolute path with //event to force it to start at the beginning of the document rather than at the current position?

Dave B Support Engineer, Constant Contact

This worked for me...

You need to do something like this...

XmlDocument doc = new XmlDocument();
doc.LoadXml(contactXml);
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");

XmlNode result = contactXml.SelectSingleNode("//atom:entry", nsmgr);

Good luck!
Bill

Bill Mild
www.Mild.net

RE:This worked for me...

Thanks Bill! That was a great namespace reference.

Dave B Support Engineer, Constant Contact

xml

works quite well thank you.

I had some strange errors but

I had some strange errors but now everything is ok.