[英]Exception on XMLDocument.LoadXml only for specific iso characters
[英]XmlDocument.LoadXml() throws an exception of type ComException
我正在嘗試解析從此鏈接返回的xml文檔,但我得到一個ComException
類型的異常, ComException
帶有以下消息:
Error HRESULT E_FAIL has been returned from a call to a COM component.
這是代碼:
try
{
//...
string EPGXML = await DownloadAsync(url);
var xmldoc = new XmlDocument();
xmldoc.LoadXml(EPGXML); //this line throws the exception
//...rest of the code
}
catch (Exception)
{
//I get here...
}
能幫到我,為什么我收到這條消息,我該如何解決這個問題? 謝謝。
編輯:
我正在使用這個函數讀取XML的源代碼(也許我在這里錯了,我應該做一些事情來獲取UTF-8中的字符串,因為我沒有在調試模式下看到字符串中的德語字符(watch窗口):
private async static Task<string> DownloadPageAsync(string url)
{
try
{
HttpClientHandler handler = new HttpClientHandler();
handler.UseDefaultCredentials = true;
handler.AllowAutoRedirect = true;
handler.UseCookies = true;
HttpClient client = new HttpClient(handler);
client.MaxResponseContentBufferSize = 10000000;
HttpResponseMessage response = await client.GetAsync(url);
response.EnsureSuccessStatusCode();
string responseBody = response.Content.ReadAsString();
return responseBody;
}
catch (Exception ex)
{
return "error" + ex.Message;
}
}
您提供的XML無效,至少Firefox的說法如下:
Erreur d'analyze XML:malforméImplacement: http ://www.onlinetvrecorder.com/?dog = epg_export&format = xml&btn_ok = OK&> stations = 3SAT,ANIXE,ARD&from = 30.11.2011&to =30.11.2011Numérodeligne 218,Colonne 193:
(對不起法國人)
看得更近一點,看起來解析器打破了字符“ö”上的“Plötzlich”這個詞。
您應該使用CDATA來防止這種情況:
<![CDATA[Your text here can contain special chars]]>
不要嘗試使用html頁面加載XML文檔。 使用Html Agility Pack本來就是這樣做的。
編輯 :如果您只想將頁面的來源作為字符串,這應該可以解決問題。
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://stackoverflow.com/posts/8331002");
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string data = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
data = reader.ReadToEnd();
Console.WriteLine(data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.