Reputation: 3823
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#?
Upvotes: 0
Views: 563
Reputation: 3823
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
Upvotes: 0