xmldocument.loadxml()抛出了类型cosexception的例外
-
26-10-2019 - |
题
我正在尝试解析XML文档从 这个链接 但是我有一个类型的例外 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中获取字符串,因为我在调试模式下看不到字符串中的德语字符(请注意窗户):
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'Analysk XML:MaleméMemplacement: http://www.onlinetvrecorder.com/?aktion=epg_export&format=xml&btn_ok=ok&ok&format=xml&btn_ok=ok&>电台= 3SAT,ANIXE,ARD&从= 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敏捷包 本来是这样做的。
编辑: :如果您只想将页面的源作为字符串,那么这应该可以解决问题。
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);
不隶属于 StackOverflow