0

I am trying to get the country, lat/long, timezone etc from an api using public ip.

Below is the xml response i am getting from 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>Chennai</City>
<StateProvince>25</StateProvince>
<Country>India</Country>
<Organization/>
<Latitude>13.0833</Latitude>
<Longitude>80.28329</Longitude>
<AreaCode>0</AreaCode>
<TimeZone/>
<HasDaylightSavings>false</HasDaylightSavings>
<Certainty>90</Certainty>
<RegionName/>
<CountryCode>IN</CountryCode>
</IPInformation>

I am loading the response in xml file, from there using SelectSingleNode i am trying to get the country value. But always i am getting nullreferenceexception.

Below is the code i have tried,

if (response.StatusCode.ToString().ToLower() == "ok")
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(response.GetResponseStream());
    XmlNode msgnode = xmlDoc.DocumentElement.SelectSingleNode("//Country"); -->getting null here
    string msgname = msgnode.InnerText;                                      
}

tried below one,

String country = xmlDoc.SelectSingleNode("IPInformation/Country").Value;

SelectSingleNode always return a null value

Full stactrace:

at SingleScanPalletTag.MobileClient.ScanOutMenu.GetGeoLocation()
   at SingleScanPalletTag.MobileClient.ScanOutMenu.button1_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
   at System.Windows.Forms.Form.ShowDialog()
   at SingleScanPalletTag.MobileClient.Program.Main()

enter image description here

Can any one tell me how to get the country,latitude and longitude value from the above xml.

Please help me.

user2681579
  • 1,413
  • 2
  • 23
  • 50
  • It's unclear from your question and code comments exactly where the `NullReferenceException` is being thrown from. Does the `SelectSingleNode` line throw an exception or return a `null` that leads to a `NullReferenceException` in the next line? – adv12 Jan 13 '16 at 17:46
  • @adv12 SelectSingleNode return a null value – user2681579 Jan 13 '16 at 17:56
  • Please Show us the full stacktrace. – etalon11 Jan 13 '16 at 17:58
  • @etalon11 added full stacktrace in question – user2681579 Jan 13 '16 at 18:02
  • If you put a watch on xmlDoc, is the xml you expect present? – StingyJack Jan 13 '16 at 18:02
  • 1
    County is not a node. It's a simple element. You could even serialize the XML into an object. I personally prefer this way. So if your XML has always the same structure, You could make a class of that Type and create an object with serialization. – etalon11 Jan 13 '16 at 18:06
  • @StingyJack xml is not present at xmlDoc.DocumentElement, but i can see it on xmlDoc.innerXml, i have attached the screen for reference – user2681579 Jan 13 '16 at 18:12
  • @user2681579 did you have success? – etalon11 Jan 14 '16 at 18:45

2 Answers2

1

What about to user XDocument() from System.Xml.Linq? Assuming that this xml file isn't a tree...

XDocument xmlDoc = new XDocument();
var sr = new System.IO.StreamReader(response.GetResponseStream())
xmlDoc = XDocument.Parse(sr.ReadToEnd());

var strCountry = xmlDoc.Root.Element("Country");

if the xml have children, use xmlDoc.Root.Descendants()

0

If I had more reputation I would have added a comment pointing you to this this StackOverflow article but I can't create comments yet so it has to be an answer.

This is probably a duplicate so if someone can flag it, please do.

This has to do with the namespaces, specifically the xmlns="http://ws.cdyne.com/" part.

Create a XmlNamespaceManager and add the namespace.

XmlDocument xmlDoc = new XmlDocument();
XmlNamespaceManager namespaces = new XmlNamespaceManager(xmlDoc.NameTable);
namespaces.AddNamespace("ns", "http://ws.cdyne.com/");
xmlDoc.Load(response.GetResponseStream());
XmlNode msgnode = xmlDoc.DocumentElement.SelectSingleNode("/ns:IPInformation/ns:Country",namespaces);
string msgname = msgnode.InnerText;
Community
  • 1
  • 1
Exxoff
  • 161
  • 1
  • 9