4
I want to take the Location of an external IP and for that I used a site where I simply put the IP I want and it returns the XML with the information.
Example:
freegeoip.net/xml/4.2.2.2
that is to say:
freegeoip.net/[tipo]/[ip]
For this I am taking all the characters of 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 tried to take the \n\t
that appear on the string site, I have tried to work with other functions of the XElement
and I have also searched for other classes but most work with file and not with string.
What’s the matter?
– Laerte
It generates an Exception at: country = xml. Attribute("Countryname"). Value; and dai does not pass.
– Amzero