首页 > 解决方案 > HtmlDocument.Save (HtmlAgilityPack) 输出不完整的文档

问题描述

我们正在使用HtmlAgilityPack保存 HTML ...输出正在修剪,不明白为什么。

我们用来创建导出的代码:

var doc = new HtmlDocument();

string html = "<head>";

html += "<title>Page Title</title>";      
html += "<style>" + style + "</style>";
html += "</head><body>";
html += body; // string is not very long
html += "<script>" + js + "</script>";   
html += "</body>";

FileStream sw = new FileStream(html_file, FileMode.Create);
doc.LoadHtml(html);
doc.Save(sw);
sw.Close();

导出的文件body被修剪。我们做错了什么?

完整的字符串非常小且直截了当,它不包含脚本、特殊字符,没有那种...导出在标题后第二部分的“附加费用”标题中间被修剪...

<div class="page-body">
                    <div class="top-title">1.Bill Summary <small style="font-size:14px;">1/2</small></div>
                    <div class="title" string="Device">
                        Period And Contract Information
                    </div>
                    <table class="partial">
                        <tr><td class="property">Maximum Half Hourly Demand:</td><td class="value">47,000 KWh</td></tr>
                        <tr><td class="property">Minimum Monthly Load Factor:</td><td class="value">57.2%</td></tr>
                        <tr><td class="property">Actual Maximum Demand:</td><td class="value">40,843 KWh</td></tr>
                        <tr><td class="property">Actual Load Factor:</td><td class="value">69.2%</td></tr>
                        <tr><td class="property">Period-to-date availability</td><td class="value">95.8%</td></tr>
                        <tr><td class="property">Contract Discount</td><td class="value">0.00%</td></tr>
                        <tr><td class="property">Contract Discount - Peak</td><td class="value">0.00%</td></tr>
                        <tr><td class="property">Contract Discount - Shoulder</td><td class="value">0.00%</td></tr>
                        <tr><td class="property">Contract Discount - Off Peak</td><td class="value">0.00%</td></tr>
                    </table>
                    <div class="title">
                        Bill Summary
                    </div>
                    <table class="partial">
                        <tr><td class="property">Energy Consumption</td><td class="value">7,072,662.46 ILS</td></tr>
                        <tr><td class="property">Fixed Fee to BB</td><td class="value">5,698.48 ILS</td></tr>
                        <tr><td class="property">Power Factor Fee to BB</td><td class="value"></td></tr>
                        <tr><td class="property">Other Fees to BB</td><td class="value"></td></tr>
                        <tr><td class="property">Min. Monthly Quantity charge</td><td class="value">66,791,095.60 ILS</td></tr>
                        <tr><td class="property">Additional Charges</td><td class="value">0.00 ILS</td></tr>
                        <tr><td class="property">Interest on Arrears</td><td class="value">0.00 ILS</td></tr>
                    </table>
                    <div class="title total">
                        <span style="display: inline-block;width: 280px;">Total Bill</span><b>7,078</b>
                    </div>
                    <table class="partial">
                        <tr><td class="property">Monthly Discount</td><td class="value">371</td></tr>
                        <tr><td class="property">Bill For Energy</td><td class="value">7,444</td></tr>
                    </table>
                </div>

标签: c#html-agility-pack

解决方案


不确定您使用的是哪个版本的 .NET/HtmlAgilityPack。我能够在 .NET 4.0/HtmlAgilityPack 1.3.0.0 上重现它,但不确定这些版本是否正确。

无论如何,它看起来是某种 HtmlAgilityPack 错误,在StreamWriter没有设置AutoFlush为 true 的情况下创建。因此,它关闭流编写器而不刷新它。

好消息是您可以自己传递它StreamWriter而不是Stream.

您的代码根据我得到的结果进行了调整:

var doc = new HtmlDocument();

string html = "<head>";

html += "<title>Page Title</title>";      
html += "<style>" + style + "</style>";
html += "</head><body>";
html += body; // string is not very long
html += "<script>" + js + "</script>";   
html += "</body>";

doc.LoadHtml(html);
using(FileStream fs = new FileStream(html_file, FileMode.Create))
using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8) { AutoFlush = true }) {
    doc.Save(sw);
    // You don't need to Close the stream by yourself, Dispose() will do the work
    // sw.Close();
}

请注意,我无法在最新版本的 .NET/HtmlAgilityPack 上重现它。


推荐阅读