Using MemoryStream to write out to XML

会有一股神秘感。 提交于 2019-11-30 19:33:16

The MemoryStream version is wasteful on this occasion. MemoryStream is useful if you want to perform Stream-like work, but don't want an actual file. If you are writing a file, then just write to the file. This avoids the need to buffer all the data in memory.

It is true that the memory stream approach is wasteful for simple operations but it is very useful for cases such as saving the xml as encrypted file, as compressed file etc.

This is overkill and a waste.

The two key approaches are based on

  1. You do not know the full structure of the document until the end.
  2. As you 'create' the parts of the document you know that they are the final form of this part of the document.

the first requires the creation of an in memory model of the document (for which the DOM is model is designed). Once you're finished with it simply writing directly to the file stream is fine.

The second allows you to save considerable memory and complexity and simply use XmlWriter which can point directly to the end stream (in this case a file stream).

At no stage is it necessary to use a MemoryStream

If you (for some reason) want to ensure the XmlWriter succeedes (i.e. otherwise the file might be truncated, but in most cases this will be due to - as mentioned - not closing tags) you can use a temporary file stream, fx something similar to this:

public class TempFileStream : FileStream
{

    public TempFileStream(Action<string> onClose)
        : base(Path.GetTempFileName(), FileMode.OpenOrCreate, FileAccess.ReadWrite)
    {
        this.CloseDelegate = onClose;
    }

    protected Action<string> CloseDelegate 
    {
        get;
        set;
    }

    public override void Close()
    {
        base.Close();
        if (File.Exists(this.Name))
        {
            this.CloseDelegate(this.Name);
        }
    }

}

The used like:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

using (TempFileStream tfs = new TempFileStream(f => File.Move(f, filename))
using (XmlWriter writer = XmlWriter.Create(tfs, settings))
{
    writer.WriteStartDocument(true);
    writer.WriteStartElement("parentelement");
    writer.WriteEndElement();
    writer.WriteEndDocument();
}

This won't consume a lot of memory (of course it only makes/makes more sense when the resulting XML is large)

I think that using a memory stream would be useful if creating the document in a web app or web service. The file location may conflict with another process that could be running the same processing which might cause invalid results. In a memory stream the processing should be segregated.

You do not have to use a MemoryStream to use the XmlWriter. The XmlWriter can directly write to a file; you can use another overload of the XmlWriter.Create method, which takes a filename as an argument, or instead of writing to a MemoryStream, you could also write to an XmlTextWriter or a FileStream.

So, you 2nd codesnippet could be written as:

using( FileStream fs = ... )
{
    XmlWriter writer = XmlWriter.Create (fs);
}

AFAIK, the XmlWriter will not protect you from creating a non-wellformed Xml.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!