accessing descendant elements from an xml returned null using linq to xml in c#
<p>Please people help me out I need to consume a web service that returns an xml from my application, The code that downloads xml works fine, but I need to extract values from the xml file, but I keep getting a null return value from the code, precisely the GetLocationFromXml() method is the method returning null, the GetLocationAsXMLFromHost() method works fine.</p>

<p>this is the complete class</p>

<pre><code>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AMIS.Core.DTOs;
using System.Net;
using System.Xml.Linq;
using System.Xml;
using System.Linq;

public class GeoLocationService
{
   private string _hostWebSite = "http://api.hostip.info";
    private readonly XNamespace _hostNameSpace = "http://www.hostip.info/api";
    private readonly XNamespace _hostGmlNameSpace = "http://www.opengis.net/gml";

public LocationInfo GetLocationInfoFromIPAddress(string userHostIpAddress)
{
    IPAddress ipAddress = null;
    IPAddress.TryParse(userHostIpAddress, out ipAddress);
    string xmlData = GetLocationAsXMLFromHost(ipAddress.ToString());
    LocationInfo locationInfo = GetLocationFromXml(xmlData);
    return locationInfo;
}

private string GetLocationAsXMLFromHost(string userHostIpAddress)
{
    WebClient webClient= new WebClient();
    string formattedUrl = string.Format(_hostWebSite + "/?ip={0}", userHostIpAddress);
    var xmlData = webClient.DownloadString(formattedUrl);
    return xmlData;
}

private LocationInfo GetLocationFromXml(string xmlData)
{
    LocationInfo locationInfo = new LocationInfo();
    var xmlResponse = XDocument.Parse(xmlData);
    var nameSpace = (XNamespace)_hostNameSpace;
    var gmlNameSpace = (XNamespace)_hostGmlNameSpace;

    try
    {
        locationInfo = (from x in xmlResponse.Descendants(nameSpace + "Hostip")
                        select new LocationInfo
                        {
                            CountryName = x.Element(nameSpace + "countryName").Value,
                            CountryAbbreviation = x.Element(nameSpace + "countryAbbrev").Value,
                            LocationInCountry = x.Element(gmlNameSpace + "name").Value
                        }).SingleOrDefault();
    }
    catch (Exception)
    {
        throw;
    }
    return locationInfo;
}
}
</code></pre>

<p>and the xml file is below</p>

<pre><code>&lt;?xml version="1.0" encoding="iso-8859-1"?&gt;
&lt;HostipLookupResultSet version="1.0.1" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd"&gt;
&lt;gml:description&gt;This is the Hostip Lookup Service&lt;/gml:description&gt;
&lt;gml:name&gt;hostip&lt;/gml:name&gt;
&lt;gml:boundedBy&gt;
&lt;gml:Null&gt;inapplicable&lt;/gml:Null&gt;
&lt;/gml:boundedBy&gt;
&lt;gml:featureMember&gt;
&lt;Hostip&gt;
  &lt;ip&gt;41.78.8.3&lt;/ip&gt;
  &lt;gml:name&gt;(Unknown city)&lt;/gml:name&gt;
  &lt;countryName&gt;NIGERIA&lt;/countryName&gt;
  &lt;countryAbbrev&gt;NG&lt;/countryAbbrev&gt;
  &lt;!-- Co-ordinates are unavailable --&gt;
  &lt;/Hostip&gt;
&lt;/gml:featureMember&gt;
&lt;/HostipLookupResultSet&gt;
</code></pre>
