(一)从网络找到一份省市对应表,到县级市一级(比较老),txt格式,如下图
(二)读取数并整理到一个list列表中
1)分别定义城市类和省类
public class Province { public string provinceName; public List<City> cities; public Province(string provinceName) { this.provinceName = provinceName; cities = new List<City>(); } } public class City { public string cityName; public List<string> subCities; public City(string cityName) { this.cityName = cityName; subCities = new List<string>(); } }
2)读取信息并分类
private List<Province> provinces; void ReadFile() { string file = @"D:cityInfo.txt"; using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) { StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default); string info = sr.ReadLine(); while (info != null) { string[] infoSplit = info.Split(' '); InsetItem(infoSplit); //string newStr = i+":"+ infoSplit[0] + "-" + infoSplit[1] + "-" + infoSplit[2]; //Debug.Log(newStr); info = sr.ReadLine(); } sr.Dispose(); sr.Close(); fs.Dispose(); fs.Close(); } }
void InsetItem(string[] item)
{
Province province = CatchProvince(item[0]);
if(province==null)
{
province = new global::Province(item[0]);
provinces.Add(province);
}
City city = CatchCity(province, item[1]);
if(city==null)
{
city = new global::City(item[1]);
province.cities.Add(city);
}
city.subCities.Add(item[2]);
}
Province CatchProvince(string provinceName)
{
Province province = null;
//bool isFound = false;
foreach(var p in provinces)
{
if(p.provinceName == provinceName)
{
province = p;
//isFound = true;
break;
}
}
return province;
}
City CatchCity(Province province,string cityName)
{
City city = null;
foreach(var c in province.cities)
{
if(c.cityName == cityName)
{
city = c;
break;
}
}
return city;
}
3)输出省市信息以备用
void OutputInfoXml() { XmlDocument cityXml = new XmlDocument(); XmlDeclaration xmlDeclaration = cityXml.CreateXmlDeclaration("1.0", "gb2312", ""); cityXml.AppendChild(xmlDeclaration); XmlElement xmlElem = cityXml.CreateElement("Menu"); cityXml.AppendChild(xmlElem); for(int i=0;i<provinces.Count;i++) { XmlElement provinceElem = cityXml.CreateElement("Province"); provinceElem.SetAttribute("Name", provinces[i].provinceName); xmlElem.AppendChild(provinceElem); for(int j=0;j<provinces[i].cities.Count;j++) { XmlElement cityElem = cityXml.CreateElement("City"); cityElem.SetAttribute("Name", provinces[i].cities[j].cityName); provinceElem.AppendChild(cityElem); for(int k=0;k<provinces[i].cities[j].subCities.Count;k++) { XmlElement subcityElem = cityXml.CreateElement("Subcity"); subcityElem.SetAttribute("Name", provinces[i].cities[j].subCities[k]); cityElem.AppendChild(subcityElem); } } } cityXml.Save(exportFile); }
(三)提供两个公共方法来获取省份以及省份对应的城市
public List<Province> GetProvices() { return provinces; } public List<City> GetCities(int index) { if (index >= provinces.Count) return null; return provinces[index].cities; }
(四)将结果输出到xml文件后续使用
void OutputInfoXml() { XmlDocument cityXml = new XmlDocument(); XmlDeclaration xmlDeclaration = cityXml.CreateXmlDeclaration("1.0", "gb2312", ""); cityXml.AppendChild(xmlDeclaration); XmlElement xmlElem = cityXml.CreateElement("Menu"); cityXml.AppendChild(xmlElem); for(int i=0;i<provinces.Count;i++) { XmlElement provinceElem = cityXml.CreateElement("Province"); provinceElem.SetAttribute("Name", provinces[i].provinceName); xmlElem.AppendChild(provinceElem); for(int j=0;j<provinces[i].cities.Count;j++) { XmlElement cityElem = cityXml.CreateElement("City"); cityElem.SetAttribute("Name", provinces[i].cities[j].cityName); provinceElem.AppendChild(cityElem); for(int k=0;k<provinces[i].cities[j].subCities.Count;k++) { XmlElement subcityElem = cityXml.CreateElement("Subcity"); subcityElem.SetAttribute("Name", provinces[i].cities[j].subCities[k]); cityElem.AppendChild(subcityElem); } } } cityXml.Save(exportFile); }
txt以及xml地址:
https://files.cnblogs.com/files/llstart-new0201/%E4%B8%AD%E5%9B%BD%E5%9F%8E%E5%B8%82%E4%BF%A1%E6%81%AF.zip