4

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

When I substitute string s for

//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?

Community
  • 1
  • 1
marcin32
  • 83
  • 2
  • 6
  • 2
    Are you serious parsing html with xml parser? – Kugel Jan 06 '12 at 14:52
  • No, I am not trying to parse html with xml parser, look again. – marcin32 Jan 06 '12 at 15:15
  • XDocument.Load is a xml parser :) – Ku6opr Jan 06 '12 at 15:22
  • But string s contains only xml valid data, it works even without `""` which i removed at some point. @HenkHolterman the e.Result displays ok via VS Html Viewer, but i just noticed it displays the s content with error via xml viewer, and i really don't see why. – marcin32 Jan 06 '12 at 15:39

1 Answers1

6

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

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();

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

Ku6opr
  • 8,126
  • 2
  • 24
  • 31
  • No, this doesn't help, it still throws NotSupportedException. Maybe you tried this code in a normal c# application, it works just fine in not-wp7 applications. – marcin32 Jan 06 '12 at 15:28
  • Oh, I didn't noticed that those characters were in there, I just assumed they would't use them. Thanks for help and tolerance. – marcin32 Jan 06 '12 at 15:48
  • Just a quick note. Since strings are immutable in C# using StringBuilder may be a better option :) – letstango Jan 21 '12 at 10:53