我想解析XML數據使用XDocument.Parse wchich拋出NotSupportedException,就像在主題:Is XDocument.Parse different in Windows Phone 7?和我更新我的代碼根據張貼的建議,但它仍然沒有幫助。前段時間我使用類似(但更簡單)的方法解析RSS,並且工作得很好。爲什麼XDocument.Parse拋出NotSupportedException?
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的嗎? – Kugel 2012-01-06 14:52:13
不,我不想用xml解析器解析html,再看看。 – marcin32 2012-01-06 15:15:52
XDocument.Load是一個xml解析器:) – Ku6opr 2012-01-06 15:22:46