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

4

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.

Example:

freegeoip.net/xml/4.2.2.2

that is:

freegeoip.net/[tipo]/[ip]

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 \n\t 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.     

asked by anonymous 18.10.2016 / 14:54

2 answers

1

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

Return example:

<Response>
    <IP>8.8.8.8</IP>
    <CountryCode>US</CountryCode>
    <CountryName>United States</CountryName>
    <RegionCode>CA</RegionCode>
    <RegionName>California</RegionName>
    <City>Mountain View</City>
    <ZipCode>94035</ZipCode>
    <TimeZone>America/Los_Angeles</TimeZone>
    <Latitude>37.386</Latitude>
    <Longitude>-122.0838</Longitude>
    <MetroCode>807</MetroCode>
</Response>

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

               })
              .FirstOrDefault();



    return "País: " 
            + xml.CountryName + "Estado: " 
            + xml.RegionName + "Cidade: " 
            + xml.City;    
}
    
18.10.2016 / 15:20
0

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/4.2.2.2");
        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: link

    
18.10.2016 / 15:25