在ios项目中遇到这样一个问题,需要读取远程的xml文件,进行解析,但是目标xml是gb2312编码的,用System.Text.Encoding.GetEncoding("gb2312")并不奏效。提示“Encoding name 'GB2312' not supported”,查了下才知道GetEncoding只支持下面四个属性:
utf-8 UTF8Encoding utf-16 UnicodeEncoding (Little-endian) utf-16BE UnicodeEncoding (Big-endian) utf-16LE
后来查了下才知道,ios中不支持gb2312编码,需要另寻他法。最终找到了,文件下载:http://pan.baidu.com/s/1mgDgb48
下载解压出来是2个文件,放入你的程序中,demo代码如下:
private void DownloadXML(string coursenumber) { var webClient = new WebClient(); webClient.DownloadStringCompleted += (s, e) = >{ var text = e.Result; text = text.Replace("=\"gb2312\"", "=\"UTF-8\""); string localFilename = "cc.xml"; string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); string localPath = Path.Combine(documentsPath, localFilename); File.WriteAllText(localPath, text); InvokeInBackground(() = >{ //XmlDocument doc = new XmlDocument (); //doc.Load (localPath); //XElement xe = XElement.Load (localPath); //var x = xe.Elements ("course").Cast<CourseVido.Course> (); Console.WriteLine("ok"); }); }; string xmlurl = string.Format("http://......./mobile/{0}/MP4/lessonPAD.xml", coursenumber); Gb2312Encoding GB2312 = new Gb2312Encoding(); webClient.Encoding = GB2312; webClient.DownloadStringAsync(new Uri(xmlurl)); }
另一处理方式:
//下载文件 WebClient client = new WebClient (); //Gb2312Encoding gb2312 = new Gb2312Encoding (); //client.Encoding = gb2312; client.DownloadFile (xmlurl, localPath); Stream str = client.OpenRead (xmlurl); //StreamReader reader = new StreamReader (str); byte[] mbyte = new byte[str.Length + 1]; int allmybyte = mbyte.Length; int startmbyte = 0; while (allmybyte > 0) { int m = str.Read (mbyte, startmbyte, allmybyte); if (m == 0) break; startmbyte += m; allmybyte -= m; } //编码处理 Gb2312Encoding gb2312 = new Gb2312Encoding (); string chinesestr = gb2312.GetString (mbyte, 0, mbyte.Length); chinesestr = chinesestr.Replace ("=\"gb2312\"", "=\"UTF-8\""); File.WriteAllText (localPath, chinesestr); str.Close (); //读取xml对象 XmlSerializer serializer = new XmlSerializer (typeof(CourseVido.course)); using (TextReader reader = new StreamReader (localPath)) { result = serializer.Deserialize (reader) as CourseVido.course; } return result;