内存流为空

发布于 2024-11-09 21:17:05 字数 1110 浏览 4 评论 0原文

我需要从不同的来源(函数)生成一个巨大的 xml 文件。我决定使用 XmlTextWriter,因为它使用的内存比 XmlDocument 少。

首先,使用底层 MemoryStream 启动一个 XmlWriter

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false, false));
xmlWriter.Formatting = Formatting.Indented;

然后我将 XmlWriter (注意 xml writer 直到最后都保持打开状态)传递给一个函数生成XML文件的开头:

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement();

// xmlWriter.WriteEndElement(); // Do not write the end of root element in first function, to add more xml elements in following functions

xmlWriter.WriteEndDocument();
xmlWriter.Flush();

但我发现底层内存流是空的(通过将字节数组转换为字符串并输出字符串)。有什么想法吗?

另外,我有一个关于如何从不同来源(函数)生成巨大 xml 文件的一般问题。我现在要做的就是保持 XmlWriter 对每个函数打开(我假设底层内存流也应该打开)并进行写入。在第一个函数中,我没有写根元素的结尾。在最后一个函数之后,我通过以下方式手动添加根元素的末尾:

string endRoot = "</Root>";
byte[] byteEndRoot = Encoding.ASCII.GetBytes(endRoot);
ms.Write(byteEndRoot, 0, byteEndRoot.Length); 

不确定这是否有效。

多谢!

I need to generate a huge xml file from different sources (functions). I decide to use XmlTextWriter since it uses less memory than XmlDocument.

First, initiate an XmlWriter with underlying MemoryStream

MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false, false));
xmlWriter.Formatting = Formatting.Indented;

Then I pass the XmlWriter (note xml writer is kept open until the very end) to a function to generate the beginning of the XML file:

xmlWriter.WriteStartDocument();

xmlWriter.WriteStartElement();

// xmlWriter.WriteEndElement(); // Do not write the end of root element in first function, to add more xml elements in following functions

xmlWriter.WriteEndDocument();
xmlWriter.Flush();

But I found that underlying memory stream is empty (by converting byte array to string and output string). Any ideas why?

Also, I have a general question about how to generate a huge xml file from different sources (functions). What I do now is keeping the XmlWriter open (I assume the underlying memory stream should open as well) to each function and write. In the first function, I do not write the end of root element. After the last function, I manually add the end of root element by:

string endRoot = "</Root>";
byte[] byteEndRoot = Encoding.ASCII.GetBytes(endRoot);
ms.Write(byteEndRoot, 0, byteEndRoot.Length); 

Not sure if this works or not.

Thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浅唱ヾ落雨殇 2024-11-16 21:17:05

从技术上讲,每个问题应该只问一个问题,所以我只会回答第一个问题,因为目前对我来说这只是对 SO 的快速访问。

您需要在尝试之前调用 Flush我认为是从 Stream 中读取的。

编辑
只是从下面的评论中冒出我的第二个预感,以证明这里接受的答案是正确的。

除了调用 Flush 之外,如果从流中读取是使用 Read 方法及其兄弟,则必须首先将流中的位置重置回开头。否则不会读取任何字节。

ms.Position = 0; /*reset Position to start*/
StreamReader reader = new StreamReader(ms); 
string text = reader.ReadToEnd(); 
Console.WriteLine(text); 

Technically you should only ask one question per question, so I'm only going to answer the first one because this is just a quick visit to SO for me at the moment.

You need to call Flush before attempting to read from the Stream I think.

Edit
Just bubbling up my second hunch from the comments below to justify the accepted answer here.

In addition to the call to Flush, if reading from the Stream is done using the Read method and its brethren, then the position in the stream must first be reset back to the start. Otherwise no bytes will be read.

ms.Position = 0; /*reset Position to start*/
StreamReader reader = new StreamReader(ms); 
string text = reader.ReadToEnd(); 
Console.WriteLine(text); 
桃酥萝莉 2024-11-16 21:17:05

也许您需要在检查内存流之前对 xml 流调用 Flush()。

Perhaps you need to call Flush() on the xml stream before checking the memory streazm.

勿忘初心 2024-11-16 21:17:05

确保在检查内存流之前对 XmlTextWriter 调用 Flush。

Make sure you call Flush on the XmlTextWriter before checking the memory stream.

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