/// <summary> /// 中文编码到Usas转换. /// </summary> /// <param name="inputString">输入字符串.</param> /// <returns></returns> protected virtual byte[] GbToUsas(string inputString) { StringBuilder sb = new StringBuilder(inputString); foreach (Match m in Regex.Matches(inputString, @"[\u4e00-\u9fa5]+")) { sb.Replace(m.Value, string.Format(@"{0}{1}", Cn2PyUtil.FullConvert(m.Value), m.Value)); } byte[] org = Encoding.GetEncoding("gb2312").GetBytes(sb.ToString()); List <byte> result = new List <byte>(); bool flag = false; foreach (byte b in org) { if (!flag && b > 128) { result.AddRange(new byte[] { 0x1B, 0x0E }); flag = true; } else if (flag && b <= 128) { result.AddRange(new byte[] { 0x1B, 0x0F }); flag = false; } if (flag) { result.Add((byte)(b - 128)); } else { result.Add(b); } } if (flag) { result.AddRange(new byte[] { 0x1B, 0x0F }); } return(result.ToArray()); }
/// <summary> /// Reads the city. /// </summary> /// <param name="ParentCity">The parent city.</param> private void ReadCity(WeatherCity ParentCity, List <WeatherCity> collection) { HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(string.Format(@"http://www.weather.com.cn/data/list3/city{0}.xml", ParentCity.CityId)); using (HttpWebResponse myRes = (HttpWebResponse)myReq.GetResponse()) { using (Stream stream = myRes.GetResponseStream()) { StreamReader sr = new StreamReader(stream, Encoding.UTF8); //250401|衡阳,250402|衡山,250403|衡东,250404|祁东,250405|衡阳县,250406|常宁,250407|衡南,250408|耒阳,250409|南岳 foreach (Match m in Regex.Matches(sr.ReadToEnd(), @"([0-9]+)\|([\u4E00-\u9FA5 0-9]+)", RegexOptions.IgnoreCase)) { WeatherCity subCity = new WeatherCity() { ParentId = ParentCity.CityId, CityId = m.Groups[1].Value, CityName = m.Groups[2].Value, CityPinYin = Cn2PyUtil.FullConvert(m.Groups[2].Value) }; if (!Regex.IsMatch(subCity.CityName, @"^[0-9]+$")) { ReadCity(subCity, collection); } collection.Add(subCity); } sr.Dispose(); } } }