【发布时间】:2011-11-30 17:57:03
【问题描述】:
我正在尝试解析从 this link 返回的 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;
}
}
【问题讨论】:
-
这段代码是否在(非主)线程上运行?
-
当您使用 .NET 的预发布版本时,最好提及(标记)。
-
您提供的链接不是有效的 XML,尽管这通常会引发 XmlException。不过,您是否尝试过加载好的 XML?
-
我在我的 MetroStyle 应用程序的 WinRT 代码中使用它。这段代码在主线程上异步运行。
标签: c# .net xml windows-runtime