Reputation: 359
When you run the following code, StatusCode is returned as null. What am I doing wrong?
var xml = @"<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://earth.google.com/kml/2.0'>
<Response>
<name>The Name</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
</Response>
</kml>";
XmlDocument XmlDoc = new XmlDocument();
ASCIIEncoding Enc = new System.Text.ASCIIEncoding();
using (MemoryStream Stream = new MemoryStream(Enc.GetBytes(xml)))
{
XmlDoc.Load(Stream);
}
XmlElement Root = XmlDoc.DocumentElement;
XmlNamespaceManager XmlNS = new XmlNamespaceManager(XmlDoc.NameTable);
XmlNS.AddNamespace("default", Root.NamespaceURI);
XmlNode XmlResults = Root.SelectSingleNode("//default:Response", XmlNS);
XmlNode StatusCode = XmlResults.SelectSingleNode("Status/code");
Thanks in advance!
Upvotes: 0
Views: 1030
Reputation: 21615
You also need to supply the namespace to the elements further on, because they too are in the namespace.
XmlNode xmlResults = Root.SelectSingleNode("//default:Response", xmlNS);
XmlNode statusCode = XmlResults.SelectSingleNode("default:Status/default:code",
xmlNS);
Upvotes: 3