为什么XDocument.Parse抛出NotSupportedException? [英] Why does XDocument.Parse throw NotSupportedException?

查看:84
本文介绍了为什么XDocument.Parse抛出NotSupportedException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XDocument解析xml数据.解析wchich引发NotSupportedException,就像在主题中一样:

I am trying to parse xml data using XDocument.Parse wchich throws NotSupportedException, just like in topic: Is XDocument.Parse different in Windows Phone 7? and I updated my code according to posted advice, but it still doesn't help. Some time ago I parsed RSS using similar (but simpler) method and that worked just fine.

public void sList()
        {

            WebClient client = new WebClient();

            client.Encoding = Encoding.UTF8;
            string url = "http://eztv.it";
            Uri u = new Uri(url);
            client.DownloadStringAsync(u);
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);


        }

    private void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            string s = e.Result;
            s = cut(s);

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;


            XDocument document = null;// XDocument.Parse(s);//Load(s);
            using (XmlReader reader = XmlReader.Create(new StringReader(e.Result), settings))
            {
                document = XDocument.Load(reader); // error thrown here
            }

            // ... rest of code
        }
        catch (Exception ex)
        {
            MessageBox.Show( ex.Message);
        }

    }

    string cut(string s)
    {
        int iod = s.IndexOf("<select name=\"SearchString\">");
        int ido = s.LastIndexOf("</select>");

        s = s.Substring(iod, ido - iod + 9);

        return s;
    }

当我用s代替

//string s = "<select name=\"SearchString\"><option value=\"308\">10 Things I Hate About You</option><option value=\"539\">2 Broke Girls</option></select>";

一切正常,不会引发异常,那我该怎么办?

Everything works and no exception is thrown, so what do I do wrong?

推荐答案

有特殊符号,例如'&'在e.Result中.

There are special symbols like '&' in e.Result.

我只是尝试用HttpUtility.HtmlEncode()XDocument解析的符号替换此符号(除了'<','>',''以外的所有符号)

I just tried replace this symbols (all except '<', '>', '"') with HttpUtility.HtmlEncode() and XDocument parsed it

UPD:

我不想显示我的代码,但是你没有给我机会:)

I didn't want to show my code, but you left me no chance :)

 string y = "";
 for (int i = 0; i < s.Length; i++)
 {
      if (s[i] == '<' || s[i] == '>' || s[i] == '"')
      {
           y += s[i];
      }
      else
      {
           y += HttpUtility.HtmlEncode(s[i].ToString());
      }
 }
 XDocument document = XDocument.Parse(y);
 var options = (from option in document.Descendants("option")
      select option.Value).ToList();

这对我来说适用于WP7. 请不要将此代码用于html转换.我很快就写了它只是为了测试目的

It's work for me on WP7. Please, do not use this code for html conversion. I wrote it quickly just for test purposes

这篇关于为什么XDocument.Parse抛出NotSupportedException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆