Get a country's continent in C#

走远了吗. 提交于 2020-06-27 08:06:10

问题


Is there a way to find a country's continent in C#, using the RegionInfo class or any other way?

For example, you give the country name "Denmark" and it returns "Europe".


回答1:


You can store a hash table. The entries will have as key the country name and as value the continent name. Just generate this table from some public database and once you have it you can simply query it. It's also pretty fast.

http://www.geonames.org/countries/
You can get from there a database (there is an option to Download/Export)




回答2:


Nothing like that in the .NET framework. It is also rather ambiguous with geographical definitions for Eurasia and Oceania. There are 6 distinct definitions in use. You'll need your own dbase.




回答3:


There are only a few continents and only a couple of hundred countries, so it wouldn't be very hard just to build your own table. There are differences of opinion as to just what the continents are (Australia/Oceania/Australasia/Asia being a particularly prickly one at times), and whether e.g. Turkey and Russia are in Europe, Asia, or both. Still, rolling your own at least lets you make your own call on these matters.




回答4:


You can use http://products.wolframalpha.com/api/

http://www.wolframalpha.com/input/?i=continent+THECOUNTRY
http://www.wolframalpha.com/input/?i=continent+sweden




回答5:


RegionInfo doesn't store continent. But you can build a database using GeoNames.org Country Information. The data format is tab-delimited text in utf8 encoding. It's has all the information and more that you need.




回答6:


I know it's an old question but i have create a library for C# and this contains the contintent information. You can found my project here Nager.Country I hope it helps you.

A nuget package is also available install-package Nager.Country

Example

ICountryProvider countryProvider = new CountryProvider();
var countryInfo = countryProvider.GetCountry(Alpha2Code.DK);


来源:https://stackoverflow.com/questions/5450328/get-a-countrys-continent-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!