xmldocument.loadxml()タイプcomexceptionの例外をスローします
-
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'Analyze XML:malforméEmplacement: http://www.onlinetvrecorder.com/?aktion=epg_export&format=xml&btn_ok=ok&>ステーション= 3SAT、anixe、ard&from = 30.11.2011&to = 30.11.2011NumérodeLigne218、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);