Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# HtmlDocument.Save(HTMLAlityPack)输出不完整的文档_C#_Html Agility Pack - Fatal编程技术网

C# HtmlDocument.Save(HTMLAlityPack)输出不完整的文档

C# HtmlDocument.Save(HTMLAlityPack)输出不完整的文档,c#,html-agility-pack,C#,Html Agility Pack,我们正在使用HtmlAgilityPack保存HTML。。。正在修剪输出,不理解原因 我们用于创建导出的代码: var doc = new HtmlDocument(); string html = "<head>"; html += "<title>Page Title</title>"; html += "<style>" + style + "</style>"; html += "</head><

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

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

无论如何,它看起来像是某种HtmlAgilityPack bug,创建
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();
}
var doc=new HtmlDocument();
字符串html=“”;
html+=“页面标题”;
html+=“”+样式+“”;
html+=“”;
html+=正文;//绳子不是很长
html+=“”+js+“”;
html+=“”;
doc.LoadHtml(html);
使用(FileStream fs=newfilestream(html_文件,FileMode.Create))
使用(StreamWriter sw=newstreamwriter(fs,Encoding.UTF8){AutoFlush=true}){
文件保存(软件);
//您不需要自己关闭流,Dispose()将完成这项工作
//sw.Close();
}

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

您能提供
正文
的示例和修剪结果吗?这是问题最本质的部分。我相信它正在做它应该做的事情,HTML的输入字符串已经修剪好了(即没有新行、标签等-不确定此处的主体是什么),那么为什么您希望agility pack为您进行格式化呢?包括足够的代码,以允许其他人重现此问题。要获得帮助,请阅读:您应该仔细检查
文件流
在关闭时是否刷新内容。尝试在
关闭
之前调用
刷新
。它在我运行时不会修剪任何内容代码。
style
js
变量的内容是什么?PS。修剪是什么意思?文件中的内容不是全部都被删除了吗?空格/新行是否被删除了?请注意html变量连接(由您完成)结果是一长串HTML代码。你必须有一些stackoverflow的经验才能知道这类答案是什么。这个答案太棒了。谢谢!它起作用了。请向上投票这个问题,它目前在零以下,我认为这是相关的。另外,如果你能编辑你认为更适合的标题。。。因为现在的标题很真实
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();
}