Why does XDocument.Parse throw a NotSupportedException?

I am trying to parse XML data using XDocument.Parse wchich throws a NotSupportedException, as in the topic: Is XDocument.Parse different in Windows Phone 7? , and I updated my code according to the posted advice, but it still doesn't help. Some time ago I parsed RSS using a similar (but simpler) method, and this 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 the 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 there are no exceptions, so what am I doing wrong?

+4
source share
1 answer

There are special characters such as '&' in e.Result .

I just tried replacing these characters (everything except '<', '>', '"'), with HttpUtility.HtmlEncode() and XDocument parsed it

UPD:

I didn’t want to show my code, but you had 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(); 

This works for me on WP7. Please do not use this code to convert html . I quickly wrote it for testing

+6
source

Source: https://habr.com/ru/post/1389665/


All Articles