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
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>
首先,您在xml中有两个xmlns属性声明-如果删除xmlns="http://ws.cdyne.com/"
并将查询更改为/IPInformation/Latitude
,则可以返回有效的XMLNode。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.