title: Null reference exception in xml - C#
body: <p>I am trying to get the country, lat/long, timezone etc from an api using public ip.</p>
  
  <p>Below is the xml response i am getting from api,</p>
  
  <pre><code>&lt;IPInformation xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://ws.cdyne.com/"&gt;
  &lt;City&gt;Chennai&lt;/City&gt;
  &lt;StateProvince&gt;25&lt;/StateProvince&gt;
  &lt;Country&gt;India&lt;/Country&gt;
  &lt;Organization/&gt;
  &lt;Latitude&gt;13.0833&lt;/Latitude&gt;
  &lt;Longitude&gt;80.28329&lt;/Longitude&gt;
  &lt;AreaCode&gt;0&lt;/AreaCode&gt;
  &lt;TimeZone/&gt;
  &lt;HasDaylightSavings&gt;false&lt;/HasDaylightSavings&gt;
  &lt;Certainty&gt;90&lt;/Certainty&gt;
  &lt;RegionName/&gt;
  &lt;CountryCode&gt;IN&lt;/CountryCode&gt;
  &lt;/IPInformation&gt;
  </code></pre>
  
  <p>I am loading the response in xml file, from there using <code>SelectSingleNode</code> i am trying to get the country value. But always i am getting <code>nullreferenceexception</code>.</p>
  
  <p>Below is the code i have tried,</p>
  
  <pre><code>if (response.StatusCode.ToString().ToLower() == "ok")
  {
      XmlDocument xmlDoc = new XmlDocument();
      xmlDoc.Load(response.GetResponseStream());
      XmlNode msgnode = xmlDoc.DocumentElement.SelectSingleNode("//Country"); --&gt;getting null here
      string msgname = msgnode.InnerText;                                      
  }
  </code></pre>
  
  <p>tried below one,</p>
  
  <pre><code>String country = xmlDoc.SelectSingleNode("IPInformation/Country").Value;
  </code></pre>
  
  <p><code>SelectSingleNode</code> always return a null value</p>
  
  <p>Full stactrace:</p>
  
  <pre><code>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()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Uwzvn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Uwzvn.png" alt="enter image description here"></a></p>
  
  <p>Can any one tell me how to get the country,latitude and longitude value from the above xml.</p>
  
  <p>Please help me.</p>