XmlDocument.LoadXml() throws an exception of type ComException

青春壹個敷衍的年華 提交于 2019-12-11 02:58:47

问题


I'm trying to parse the xml document returned from this link but I get an exception of type ComException with the following message:

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):

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

回答1:


The XML you provided is not valid, at least that's what Firefox says:

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 :

(Sorry for the french)

Looking a bit closer, it looks like the parser breaks on the word "Plötzlich", on the character "ö".

You should use CDATA to prevent this:

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



回答2:


Do not try to load an XML Document with an html page. Use Html Agility Pack which was meant to do so.

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);


来源:https://stackoverflow.com/questions/8330619/xmldocument-loadxml-throws-an-exception-of-type-comexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!