我正在尝试使用XDocument.Parse解析XML数据,但它会抛出NotSupportedException异常,就像这个主题中所述:在Windows Phone 7中,XDocument.Parse是否不同?我根据发布的建议更新了我的代码,但还是没有帮助。之前我使用类似但更简单的方法解析RSS,并且那个方法很好用。
当我用字符串s代替时
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 version=\"1.0\" encoding=\"UTF-8\"?>"
,它也可以正常工作。@HenkHolterman通过VS Html Viewer显示e.Result正常,但我刚刚注意到它通过xml viewer显示带有错误的s内容,我真的不知道为什么。 - marcin32