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.

8
  • 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? Commented Jan 13, 2016 at 17:46
  • @adv12 SelectSingleNode return a null value Commented Jan 13, 2016 at 17:56
  • Please Show us the full stacktrace. Commented Jan 13, 2016 at 17:58
  • @etalon11 added full stacktrace in question Commented Jan 13, 2016 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. Commented Jan 13, 2016 at 18:06

2 Answers 2

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()

Sign up to request clarification or add additional context in comments.

Comments

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;

1 Comment

@user2681579 If you found any of the answers useful, please mark them as answer.

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.