HtmlDocument.Save(HtmlAgilityPack)输出不完整的文档

时间:2018-12-20 12:02:54

标签: c# html-agility-pack

我们正在使用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>

1 个答案:

答案 0 :(得分:3)

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

无论如何,创建StreamWriter而不将AutoFlush设置为true似乎是某种HtmlAgilityPack错误。因此,它会关闭流写入器而不刷新它。

好消息是您可以通过自己的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上重现它。