Reputation: 1481
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
Upvotes: 2
Views: 156
Reputation: 1475
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.
Upvotes: 0
Reputation: 3253
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>
Upvotes: 4