c# - 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>
解决方案
不确定您使用的是哪个版本的 .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 上重现它。
推荐阅读
- vba - 如果选择了组外的另一个选项按钮,则取消选择整个选项按钮组
- python - CrawlerRunner() 没有通过scrapy的管道文件
- sql - 在实体框架中使用左连接过滤
- c - Do-while 循环 scanf 在读取单个字符时读取循环两次而不是一次
- azure - 无法使用 Azure AD 进行身份验证,丢失 cookie 和重定向显示没有权限
- sql - 如何过滤从 PostgreSQL 表中的 JSON 列返回的 json 数组?
- azure - 使用 Terraform 管理基础设施的环境特定属性
- visual-studio - 如何在没有“在程序集中找不到迁移配置类型”问题的情况下创建迁移?
- angular - 如何在 Angular 和 localstorage 中检查和存储用户 ID
- javascript - 在 node.js 中的模块之间传递 mysql 连接