C # – Problems reading data from an XML returned from a site

Posted on

Question :

I want to get the location of an external IP and for that I used a site where I simply put the IP that I want and it returns the XML with the information.



that is:


For this I’m getting all the characters from this site and trying to work with a string containing an XML inside and returning what I want:

public static string getLocationIPAddress()
        string country = null;
        string state = null;
        string city = null;

        System.Net.WebClient t = new System.Net.WebClient();
        string site = t.DownloadString("https://freegeoip.net/xml/" 
                                              + getExternIPAdrress());          

        XElement xml = XElement.Parse(site);           

        country = xml.Attribute("CountryName").Value;
        state = xml.Attribute("RegionName").Value;
        city = xml.Attribute("City").Value;   

        return "País: " + country + "Estado: " + state + "Cidade: " + city;


I’ve already tried to get the nt that appears in the string site, I’ve tried to work with other functions of XElement and I’ve also looked for other classes, but most work with file and not with string.

Answer :

Return on XML might not be the best, but, there’s a way:

Return example:

    <CountryName>United States</CountryName>
    <City>Mountain View</City>

Use XDocument

public static string getLocationIPAddress()
    System.Net.WebClient t = new System.Net.WebClient();
    string site = t.DownloadString("https://freegeoip.net/xml/" 
                                               + getExternIPAdrress());

    var xml = (from d in XDocument.Parse(site).Descendants("Response")
               let ip = d.Element("IP")
               let countryCode = d.Element("CountryCode")
               let countryName = d.Element("CountryName")
               let regionCode = d.Element("RegionCode")
               let regionName = d.Element("RegionName")
               let city = d.Element("City")
               let zipCode = d.Element("ZipCode")
               let timeZone = d.Element("TimeZone")
               let latitude = d.Element("Latitude")
               let longitude = d.Element("Longitude")
               let metroCode = d.Element("MetroCode")
               select new
                   IP = ip.Value,
                   CountryCode = countryCode.Value,
                   CountryName = countryName.Value,
                   RegionCode = regionCode.Value,
                   RegionName = regionName.Value,
                   City = city.Value,
                   ZipCode = zipCode.Value,
                   TimeZone = timeZone.Value,
                   Latitude = latitude.Value,
                   Longitude = longitude.Value,
                   MetroCode = metroCode.Value


    return "País: " 
            + xml.CountryName + "Estado: " 
            + xml.RegionName + "Cidade: " 
            + xml.City;    


Using XDocument you can make your parser as follows:

public static string getLocationIPAddress()
    var result = string.Empty;

    using (var client = new System.Net.WebClient())
        var xmlResponse = client.DownloadString("https://freegeoip.net/xml/");
        var parsedDocument = XDocument.Parse(xmlResponse);

        var rootElements = from items 
            in parsedDocument.Descendants("Response")
            select items;

        var countryName = rootElements.Descendants("CountryName").FirstOrDefault();
        var regionName = rootElements.Descendants("RegionName").FirstOrDefault();
        var cityName = rootElements.Descendants("City").FirstOrDefault();

        if (countryName != null && regionName != null && cityName != null)
            result = $"País: {countryName.Value}; Estado: {regionName.Value}; Cidade: {cityName.Value};";

    return result;

I made some modifications to your methods but it returns the expected result.

You can also search a little more about XDocument and how to use it at this link:


Leave a Reply

Your email address will not be published. Required fields are marked *