关于gb2312编码的xml解析
2014-03-13 14:07
330 查看
在ios项目中遇到这样一个问题,需要读取远程的xml文件,进行解析,但是目标xml是gb2312编码的,用System.Text.Encoding.GetEncoding("gb2312")并不奏效。提示“Encoding name 'GB2312' not supported”,查了下才知道GetEncoding只支持下面四个属性:
后来查了下才知道,ios中不支持gb2312编码,需要另寻他法。最终找到了,文件下载:http://pan.baidu.com/s/1mgDgb48
下载解压出来是2个文件,放入你的程序中,demo代码如下:
另一处理方式:
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;
相关文章推荐
- 【转】Android开发之获取相册照片和获取拍照照片
- Hibernate详解(5)——Hibernate核心接口和工作原理
- 图片扩大缩小切换(类似微博图片查看)
- 职场如何做好时间管理
- nginx对PHP文件报File not found.错
- AutoCompleteTextView 自动提示功能使用详解
- 解析“extern”
- CDN动态内容的缓存技术 CSI,SSI,ESI
- 对 Linux 新手非常有用的 20 个命令
- 编译mediastreamer2/ffmpeg/linphone(x86平台)
- 1998年以来“两会”期间报告关键词及股市表现
- Surface系列平板电脑
- 第三周——长方体类
- oracle 11g data guard
- 微软在思维上的新旧交替
- 卷二 Dalvik与Android源码分析 第五章 Interpreter与JIT 5.3-C解释器 5.4--汇编解释器 图书版试读--请勿转发
- c# 中引用对象如何创建副本
- 在PC互联网时代
- oracle 11g data guard
- 关于LINUX权限-bash: ./startup.sh: Permission denied .