I'm trying to fetch a A2 ISO country code by having a full country name as an input.
For example:
var html = new WebClient().DownloadString("mysite.com");
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var result = htmlDoc.DocumentNode.SelectNodes("//span[@class='mem_loc']").ElementAt(0).InnerText;
This will give me an out put for example:
Bosnia & Herzegovina
United States
Croatia
The input would be like:
Bosnia & Herzegovina
The outputed country code would be: BA
For United States: US
Croatia: HR
And so on...
What's the best way to do this in C#?
This did it in the end:
var html = new WebClient().DownloadString("mysite.com");
HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var result = htmlDoc.DocumentNode.SelectNodes("//span[@class='mem_loc']").ElementAt(0).InnerText;
var regionFullNames = CultureInfo
.GetCultures(CultureTypes.SpecificCultures)
.Select(x => new RegionInfo(x.LCID))
;
var twoLetterName = Regex.Replace(regionFullNames.FirstOrDefault(
region => region.EnglishName.Contains(result)
).ToString(), "{.*?}","");
Provided input: United States
Output: US
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.