繁体   English   中英

为什么XDocument.Parse抛出NotSupportedException?

[英]Why does XDocument.Parse throw NotSupportedException?

我试图使用XDocument.Parse wchich解析xml数据抛出NotSupportedException,就像在主题中一样: Windows Phone 7中的XDocument.Parse是否不同? 我根据发布的建议更新了我的代码,但它仍然无济于事。 前段时间我使用类似(但更简单)的方法解析RSS,并且工作得很好。

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

当我用字符串替换

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

一切正常,没有异常被抛出,所以我做错了什么?

e.Result有像'&'这样的特殊符号。

我只是尝试用HttpUtility.HtmlEncode()替换这个符号(除了'<','>','“'之外的所有符号HttpUtility.HtmlEncode()并且XDocument解析了它

UPD:

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

 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转换 我为了测试目的而快速写了它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM