Convert XDocument to Stream

后端 未结 3 554
傲寒
傲寒 2020-12-05 09:32

How do I convert the XML in an XDocument to a MemoryStream, without saving anything to disk?

3条回答
  •  不知归路
    2020-12-05 10:22

    In .NET 4 and later, you can Save it to a MemoryStream:

    Stream stream = new MemoryStream();
    doc.Save(stream);
    // Rewind the stream ready to read from it elsewhere
    stream.Position = 0;
    

    In .NET 3.5 and earlier, you would need to create an XmlWriter based on a MemoryStream and save to that, as shown in dtb's answer.

提交回复
热议问题