Domanda

Sto cercando di analizzare il documento XML restituito da questo link ma ottengo un'eccezione di tipo ComException con il seguente messaggio:

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

Ecco il codice:

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

La prego di aiuto perché ricevo questo messaggio e come posso risolvere questo problema? Grazie.

Modifica

Sto leggendo la fonte del XML utilizzando questa funzione (forse mi sbaglio qui e devo fare qualcosa per ottenere la stringa in UTF-8, perché non vedo i personaggi tedeschi nella stringa di debug in modalità (finestra di controllo):

    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;
        }
    }
È stato utile?

Soluzione

L'XML che hai fornito non è valido, almeno questo è quello che dice di Firefox:

Erreur d'analizzare XML: formé mal Piazzola: http://www.onlinetvrecorder.com/?aktion=epg_export&format=xml&btn_ok=OK& > stazioni = 3SAT, Anixe, ARD e da = 30.11.2011 & a = 30.11.2011 Numéro de ligne 218, 193 Colonne:

(Ci scusiamo per il francese)

Guardando un po 'più da vicino, sembra che le interruzioni di parser sulla parola "Plötzlich", sulla "ö" carattere.

Si dovrebbe usare CDATA per impedire questo:

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

Altri suggerimenti

Non provare a caricare un documento XML con una pagina html. Usa Html Agility pacchetto che doveva farlo.

Modifica :. Se si desidera solo la fonte della pagina come una stringa questo dovrebbe fare il trucco

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top