HtmlDocument.Save (HtmlAgilityPack) 输出不完整的文档
HtmlDocument.Save (HtmlAgilityPack) outputs incomplete document
我们正在使用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
被修剪。我们做错了什么?
完整的字符串非常小而且直接,它不包含脚本、特殊字符、none 之类的......导出在 "Additional Charges" 标题的中间被修剪标题后的第二部分...
<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 上重现它,但不确定这些版本是否正确。
无论如何,创建 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.
上重现它
我们正在使用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
被修剪。我们做错了什么?
完整的字符串非常小而且直接,它不包含脚本、特殊字符、none 之类的......导出在 "Additional Charges" 标题的中间被修剪标题后的第二部分...
<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 上重现它,但不确定这些版本是否正确。
无论如何,创建 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.
上重现它