0

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!

2
  • You can see by looking at the syntax coloring btw that CamelCasing local variables is pretty non-standard. The pascalCased "xml" looks better, just a thought :) Commented Dec 17, 2009 at 18:59
  • I believe you have your camelCasing and PascalCasing backwards. Commented Dec 17, 2009 at 19:12

1 Answer 1

3

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.