I have a stream object, and I want to create and output xml using some kind of xml stream, based on data in my input stream. I haven’t done much work with streams, so I am trying to learn how to do this as efficiently as possible. The idea is that I do not want to load the entire input stream in memory, then create the entire output stream in memory because at some point you end up with at least double the size of the input stream taking up memory.
There must be ways so that as data is read in the input stream, the xml stream is built and the data read from the input stream is discarded. Additionally, I would like to architect it so that the xml stream isn’t built completely then passed out, but instead the xml stream can be read as it is being built. Does anyone have some code samples, or good resources for learning about this?
For writing your XML out as a stream, use XmlTextWriter.
You can give it a stream to write to. This will give you what you want (stream output) and give you a lot of flexibility. By passing the stream to use to your code as an argument, you could hook up a memory stream, or a file stream or just about anything without the XML formatting code needing to know. The XmlTextWriter will periodically flush the data (so it wont stick around unless nothing reads it from the stream), just remember to do a final flush/close at the end.
Streams are also how you will handle input. As you process that input and can decide on what XML elements to write, use the XmlTextWriter to write them and it will take care of streaming that data out to whoever will be reading it. So you end up with a loop (or loops) that is reading a bit, doing some processing, then writing to the XmlTextWriter all at once.