简体   繁体   English

XmlDocument.LoadXml()抛出ComException类型的异常

[英]XmlDocument.LoadXml() throws an exception of type ComException

I'm trying to parse the xml document returned from this link but I get an exception of type ComException with the following message: 我正在尝试解析从此链接返回的xml文档,但我得到一个ComException类型的异常, ComException带有以下消息:

Error HRESULT E_FAIL has been returned from a call to a COM component.

Here's the code: 这是代码:

        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...
        }

Could you please help me why I get this message and how can I fix this? 能帮到我,为什么我收到这条消息,我该如何解决这个问题? Thanks. 谢谢。

EDIT: 编辑:

I'm reading the source of the XML using this function (maybe I'm wrong here and I should do something to get the string in UTF-8, because I don't see the German characters in the string in debug mode (watch window): 我正在使用这个函数读取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;
        }
    }

The XML you provided is not valid, at least that's what Firefox says: 您提供的XML无效,至少Firefox的说法如下:

Erreur d'analyse XML : mal formé Emplacement : http://www.onlinetvrecorder.com/?aktion=epg_export&format=xml&btn_ok=OK& >stations=3SAT,ANIXE,ARD&from=30.11.2011&to=30.11.2011 Numéro de ligne 218, Colonne 193 : 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:

(Sorry for the french) (对不起法国人)

Looking a bit closer, it looks like the parser breaks on the word "Plötzlich", on the character "ö". 看得更近一点,看起来解析器打破了字符“ö”上的“Plötzlich”这个词。

You should use CDATA to prevent this: 您应该使用CDATA来防止这种情况:

<![CDATA[Your text here can contain special chars]]>

Do not try to load an XML Document with an html page. 不要尝试使用html页面加载XML文档。 Use Html Agility Pack which was meant to do so. 使用Html Agility Pack本来就是这样做的。

EDIT : If you just want the source of the page as a string this should do the trick. 编辑 :如果您只想将页面的来源作为字符串,这应该可以解决问题。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM