2

I have the following xml file from an API,

<IPInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ws.cdyne.com/">
<City>xxxxxx</City>
<StateProvince>12</StateProvince>
<Country>xxxxxx</Country>
<Organization/>
<Latitude>13.0833</Latitude>
<Longitude>80.28329</Longitude>
<AreaCode>0</AreaCode>
<TimeZone/>
<HasDaylightSavings>false</HasDaylightSavings>
<Certainty>90</Certainty>
<RegionName/>
<CountryCode>xx</CountryCode>
</IPInformation>

I need to get the Latitude and Longitude values from above xml and store it in a string.

I am working on c# .net 3.5 framework, I tried the below code,

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(response.GetResponseStream());
location = xmlDoc.DocumentElement.SelectSingleNode("//City");
latitude = xmlDoc.DocumentElement.SelectSingleNode("//Latitude");

I am always getting Null instead of 13.0833 and 80.28329.

Can any one tell me how to retrieve the Latitude and Longitude values from above xml.

Thanks

6
  • 1
    Deserialize it to an object of type IPInformation.. Use the OOP way.. Commented Apr 12, 2016 at 12:38
  • Good point; however, it does not explain why the curren approach does not work as expected. Commented Apr 12, 2016 at 12:40
  • 1
    What is the type of the variables latitude and longitude? The method SelectSingleNode does not parse the content of the selected node; its return type is XmlNode, which has to be process further to achieve parsing. Commented Apr 12, 2016 at 12:41
  • Just read briefly, but this might help: stackoverflow.com/questions/17696745/… Commented Apr 12, 2016 at 12:42
  • @Codor -- but the SelectSingleNode call returns null... Commented Apr 12, 2016 at 12:51

2 Answers 2

4

Your problem is the namespace. I copied your XML into a.xml and following works (LINQpad):

void Main()
{
    var a = @"c:\temp\a\a.xml";
    XmlDocument x = new XmlDocument();
    x.Load(a);

    var ns = new XmlNamespaceManager(x.NameTable);
    ns.AddNamespace("x", x.DocumentElement.NamespaceURI);
    x.DocumentElement.SelectSingleNode("//x:Longitude", ns).Dump();

}

prints

<Longitude xmlns="http://ws.cdyne.com/">80.28329</Longitude>
Sign up to request clarification or add additional context in comments.

3 Comments

I am getting the following error while build the app "Error 'System.Xml.XmlNode' does not contain a definition for 'Dump' and no extension method 'Dump' accepting a first argument of type 'System.Xml.XmlNode' could be found (are you missing a using directive or an assembly reference?)"
Dump is a LINQPad command. Try running Console.WriteLine(x.DocumentElement.SelectSingleNode("//x:Longitude", ns)) or Console.WriteLine(x.DocumentElement.SelectSingleNode("//x:Longitude", ns).InnerText);
xmlDoc.DocumentElement.SelectSingleNode("//x:Longitude", ns).InnerXml.ToString(); - worked for me.
0

For a start you have two xmlns attribute declarations in your xml - if you remove xmlns="http://ws.cdyne.com/" and change your query to /IPInformation/Latitude that gives you back a valid XMLNode.

1 Comment

No, there are no two xmlns attributes. The xmlns is the default namespace.

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.