HtmlTextWriter 处理后不会刷新?

发布于 2024-11-18 23:24:09 字数 1188 浏览 4 评论 0原文

我需要编写一些带有样式的文本(如颜色、字体),所以我决定使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寂寞笑我太脆弱 2024-11-25 23:24:09

这是 HtmlTextWriter 中的一个错误。您应该制作一个独立的测试用例,并使用 Microsoft Connect 进行报告。似乎 CloseDispose 的行为不同,这没有记录并且非常不寻常。我在 MSDN 上也找不到任何文档说明 HtmlTextWriter 是否拥有底层的所有权文本编写者 与否;即它会处理底层文本编写器还是必须处理?

编辑 2: HtmlTextWriter 声明它继承(而不是覆盖)虚拟 Dispose(bool) 方法。这意味着当前的实现显然无法使用 using 块进行清理。作为解决方法,请尝试以下操作:

using(var writer = ...make TextWriter...) 
using(var htmlWriter = new HtmlTextWriter(writer)) {

    //use htmlWriter here...

} //this should flush the underlying writer AND the HtmlTextWriter

// although there's currently no need to dispose HtmlTextWriter since
// that doesn't do anything; it's possibly better to do so anyhow in 
// case the implementation gets fixed

顺便说一句,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 that Close and Dispose 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 virtual Dispose(bool) method. This means the current implementation clearly cannot clean up with a using block. As a workaround, try this:

using(var writer = ...make TextWriter...) 
using(var htmlWriter = new HtmlTextWriter(writer)) {

    //use htmlWriter here...

} //this should flush the underlying writer AND the HtmlTextWriter

// although there's currently no need to dispose HtmlTextWriter since
// that doesn't do anything; it's possibly better to do so anyhow in 
// case the implementation gets fixed

Incidentally, new StreamWriter(XYZ, false, Encoding.UTF8) is equivalent to new 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!

雨落□心尘 2024-11-25 23:24:09

您不需要在 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.

她如夕阳 2024-11-25 23:24:09

我怀疑原因是 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 call Close() 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 the try finally block within the using statement though. As Eamon Nerbonne has pointed out, this is surely a framework bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文