HtmlTextWriter 处理后不会刷新?
我需要编写一些带有样式的文本(如颜色、字体),所以我决定使用 html。我发现HtmlTextWriter
是一个用于编写html文件的类。但是,我发现我必须手动关闭或刷新它,否则不会将任何内容写入文件。这是为什么呢? (using 语句应该在块完成时处理它)
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(new StreamWriter(
Path.Combine(EmotionWordCounts.FileLocations.InputDirectory.FullName, fileName),
false, Encoding.UTF8)))
{
try
{
htmlWriter.WriteFullBeginTag("html");
htmlWriter.WriteLine();
htmlWriter.Indent++;
htmlWriter.WriteFullBeginTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent++;
// write something using WriteFullBeginTag and WriteEndTag
// ...
} //try
finally
{
htmlWriter.Indent--;
htmlWriter.WriteEndTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent--;
htmlWriter.WriteEndTag("html");
htmlWriter.Close(); // without this, the writer doesn't flush
}
} //using htmlwriter
提前致谢。
I need to write some text with style (like color, fonts) so I decided to use html. I found that HtmlTextWriter
is a class used for writing html file. However, I found that I must manually close or flush it otherwise nothing is written to the file. Why is it? (using statement should dispose it when the block is finished)
using (HtmlTextWriter htmlWriter = new HtmlTextWriter(new StreamWriter(
Path.Combine(EmotionWordCounts.FileLocations.InputDirectory.FullName, fileName),
false, Encoding.UTF8)))
{
try
{
htmlWriter.WriteFullBeginTag("html");
htmlWriter.WriteLine();
htmlWriter.Indent++;
htmlWriter.WriteFullBeginTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent++;
// write something using WriteFullBeginTag and WriteEndTag
// ...
} //try
finally
{
htmlWriter.Indent--;
htmlWriter.WriteEndTag("body");
htmlWriter.WriteLine();
htmlWriter.Indent--;
htmlWriter.WriteEndTag("html");
htmlWriter.Close(); // without this, the writer doesn't flush
}
} //using htmlwriter
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是
HtmlTextWriter
中的一个错误。您应该制作一个独立的测试用例,并使用 Microsoft Connect 进行报告。似乎Close
和Dispose
的行为不同,这没有记录并且非常不寻常。我在 MSDN 上也找不到任何文档说明 HtmlTextWriter 是否拥有底层的所有权文本编写者 与否;即它会处理底层文本编写器还是必须处理?编辑 2:
HtmlTextWriter
声明它继承(而不是覆盖)虚拟Dispose(bool)
方法。这意味着当前的实现显然无法使用 using 块进行清理。作为解决方法,请尝试以下操作:顺便说一句,
new StreamWriter(XYZ, false, Encoding.UTF8)
相当于new StreamWriter(XYZ)
。 StreamWriter 默认情况下创建而不是追加,并且默认情况下它也使用无 BOM 的 UTF8。祝你好运 - 并且不要忘记报告错误!
This is a bug in
HtmlTextWriter
. You should make a self-contained test case and report it using Microsoft Connect. It seems thatClose
andDispose
behave differently, which isn't documented and is extremely unusual. I also can't find any documentation on MSDN stating whether the HtmlTextWriter takes ownership of the underlying textwriter or not; i.e. will it dispose the underlying textwriter or must you?Edit 2: The MSDN page on
HtmlTextWriter
states that it inherits (as opposed to overrides) the virtualDispose(bool)
method. This means the current implementation clearly cannot clean up with a using block. As a workaround, try this:Incidentally,
new StreamWriter(XYZ, false, Encoding.UTF8)
is equivalent tonew StreamWriter(XYZ)
. StreamWriter creates rather than appends by default, and it uses UTF8 without BOM by default as well.Good luck - and don't forget to report the bug!
您不需要在 using 语句中使用 try{}Finally{} 块,因为这将为您处理该对象。
You do not need to have the try{} Finally {} block within the using statement as this will dispose of the object for you.
我怀疑原因是 HtmlTextWriter 没有为 TextWriter 的
protected virtual void Dispose( bool diswriting )
方法提供重写来调用Close()
所以,你是对的,你需要您自己执行此操作 - TextWriter 的实现是空的。正如方面所指出的,您不需要在using
语句中使用try finally
块。正如 Eamon Nerbonne 指出的那样,这肯定是一个框架错误。I suspect the reason is that HtmlTextWriter does not provide an override for TextWriter's
protected virtual void Dispose( bool disposing )
method to callClose()
so, you're right, you'll need to do this yourself - TextWriter's implementation is empty. As aspect has pointed out, you don't need thetry finally
block within theusing
statement though. As Eamon Nerbonne has pointed out, this is surely a framework bug.