首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >StringWriter与StreamWriter包装MemoryStream -差异

StringWriter与StreamWriter包装MemoryStream -差异
EN

Stack Overflow用户
提问于 2011-12-01 10:36:46
回答 1查看 9.2K关注 0票数 1

我有以下代码:

代码语言:javascript
复制
        byte[] snapthotBytes, snapthotBytes2;
        string stringOutput, stringOutput2;
        IInvestigationDump investigationDump = new HtmlSnapshotDump();
        using (TextWriter writer = new StringWriter())
        {
            investigationDump.Dump(investigation, writer);
            snapthotBytes = Encoding.UTF8.GetBytes(writer.ToString());
            stringOutput = writer.ToString();
        } // end of first implementation

        using (MemoryStream stream = new MemoryStream())
        using (TextWriter writer = new StreamWriter(stream))
        {
            investigationDump.Dump(investigation, writer);
            stream.Seek(0, SeekOrigin.Begin);
            var reader = new StreamReader(stream);
            stringOutput2 = reader.ReadToEnd();
            snapthotBytes2 = stream.ToArray();
        } // end of second implementation

        // stringOutput != stringOutput2 - content wise
        // snapthotBytes != snapthotBytes2 - content wise

导言:

  • 转储方法只是遍历调查对象并呈现一个HTML (通过将StringWriter使用UTF-16编码和XML声明这一事实写入编写器),stringOutput和stringOutput2都应该具有相同的内容。转储方法签名是:

void Dump(IInvestigation investigation, TextWriter writer);

  • Dump方法只是写入编写器,没有任何条件,等等

起初,我使用了MemoryStream代码片段,因为我直接收到了byte[]代码片段,这样就更容易了。但很快我发现了一个窃听器。令人惊讶的是,stringOutput2 (由MemoryStream解决方案生产)被裁剪,它更短了!它只是以修剪过的HTML内容结束:

代码语言:javascript
复制
        <tr>
          <td>Certificate or License No</td>
          <td class="value">Friuan</td>
          <td>Place of Issue</td>
          <td class="value">Foruan</td>
        </tr>
        <tr>
          <td>Award Date</td>
          <td 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-01 10:41:44

怎么样

代码语言:javascript
复制
writer.Flush()

在试着读溪流之前?

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8339941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档