关于gb2312编码的xml解析

在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;



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值