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

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
user2681579
  • 1,413
  • 2
  • 23
  • 50
  • 1
    Deserialize it to an object of type IPInformation.. Use the OOP way.. – jegtugado Apr 12 '16 at 12:38
  • Good point; however, it does not explain why the curren approach does not work as expected. – Codor Apr 12 '16 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. – Codor Apr 12 '16 at 12:41
  • Just read briefly, but this might help: http://stackoverflow.com/questions/17696745/using-selectsinglenode-with-xpath-returns-null – rory.ap Apr 12 '16 at 12:42
  • @Codor -- but the `SelectSingleNode` call returns null... – rory.ap Apr 12 '16 at 12:51
  • BTW try this webpage for testing XPath expressions [link](http://www.freeformatter.com/xpath-tester.html#ad-output) – auburg Apr 12 '16 at 13:02

2 Answers2

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>
rbm
  • 3,243
  • 2
  • 17
  • 28
  • 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?)" – user2681579 Apr 12 '16 at 13:10
  • `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);` – rbm Apr 12 '16 at 13:13
  • xmlDoc.DocumentElement.SelectSingleNode("//x:Longitude", ns).InnerXml.ToString(); - worked for me. – user2681579 Apr 12 '16 at 13:39
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.

auburg
  • 1,373
  • 2
  • 12
  • 22