【问题标题】:Why does XDocument.Parse throw NotSupportedException?为什么 XDocument.Parse 会抛出 NotSupportedException?
【发布时间】:2012-01-07 02:40:45
【问题描述】:

我正在尝试使用 XDocument.Parse 解析 xml 数据,wchich 会抛出 NotSupportedException,就像在主题中一样:Is XDocument.Parse different in Windows Phone 7? 并且我根据发布的建议更新了我的代码,但它仍然没有帮助。前段时间我使用类似(但更简单)的方法解析 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;
    }

当我用字符串 s 代替

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

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

【问题讨论】:

  • 你是认真的用xml解析器解析html吗?
  • 不,我不是想用xml解析器解析html,再看一遍。
  • XDocument.Load 是一个 xml 解析器 :)
  • 但字符串 s 仅包含 xml 有效数据,即使没有我在某个时候删除的 "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;" 也可以工作。 @HenkHolterman e.Result 通过 VS Html 查看器显示正常,但我只是注意到它通过 xml 查看器显示错误内容,我真的不明白为什么。

标签: c# silverlight windows-phone-7 linq-to-xml


【解决方案1】:

e.Result 中有 '&' 等特殊符号。

我刚刚尝试用HttpUtility.HtmlEncode()XDocument 替换这些符号(除了''、'"')并解析它

更新:

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

 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 转换。我写得很快,只是为了测试目的

【讨论】:

  • 不,这没有帮助,它仍然会抛出 NotSupportedException。也许你在一个普通的 c# 应用程序中尝试过这段代码,它在非 wp7 应用程序中工作得很好。
  • 哦,我没有注意到那些字符在那里,我只是假设他们不会使用它们。感谢您的帮助和宽容。
  • 只是一个简短的说明。由于字符串在 C# 中是不可变的,因此使用 StringBuilder 可能是更好的选择 :)
猜你喜欢
  • 1970-01-01
  • 2011-03-23
  • 1970-01-01
  • 2021-12-20
  • 2021-03-05
  • 2021-03-19
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多