Whenever you want to sendan ordered amount of bytes to another "thingy", which needs to interpret the bytes in the same order to which it was sent, it is wise to use a System.IO.Stream class.
Using this class you declare that you don't really care where the bytes are going to, as long as the order remains the same. So both the writer and the reader of the bytes don't care whether the stream of bytes is put in a file, a piece of memory, even a papertape reader, as long as the order isn't changed, and no byte is being added or lost.
In practice: whenever you think you could write them on a file, after which you'd give the filename to someone else, you could also use a stream. Either you, or the object that ordered you to write it to the file, creates the stream with the destination of the stream (memory / papertape / file). The stream is than passed to the other, who can read the stream.
Example:
MySpecialObject has some data: MySpecialData. There are classes to serialize and deserialize this special data. These classes are not interested where the data is fysically serialized: patertape? diskette? memory. They only need to know that they can (de)serialize it by writing and reading a stream of bytes. IN that case you use an instance of System.IO.Stream.
(I write the namespaces, to make it easier to find the classes)
class MySerializer
{
private System.IO.Stream myStream = null;
public MySerializer(system.IO.Stream stream)
{
this.myStream = stream;
}
public void Serialize(object data)
{
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.SerializationXmlSerializer(data.GetType());
System.XML.XmlWriter writer = System.XML XmlWriter.Create(this.MyStream);
serializer.Serialize(writer, data);
}
}
If you look closely, you'll see that the only difference with using a file is the parameter in XMLWriter.Create()
Now the usage of this in mySpecialObject:
public void SerializeMySpecialData()
{
System.IO.Stream myStream = new System.IO.MemoryStream();
// in this case: read and write using only memory.
// if you want to write to something else, for instance a file,
// create a file stream.
// or any other subclass from Sytem.IO.Stream. The serializer won't notice
// where it is actually stored.
MySerializer mySerializer = new MySerializer(myStream);
mySerializer.Serialize(mySpecialData);
myStream.Flush();
myStream.Close();
myStream.Dispose(); // this function will probably also flush and close
}
Reading XML from a stream is similar as reading XML from a file:
public object Deserialize(System.Type expectedDataType)
{
object data = null;
System.Xml.Serialization.XmlSerializer deserializer =
new System.Xml.SerializationXmlSerializer(expectedDataType);
System.XML.XmlReader reader = System.XML XmlReader.Create(this.MyStream);
deserializer.Deserialize(reader, data);
return data;
}
Once again: the advantage is that the caller decides where the data actually is stored. If the data is stored on a different type of media the serialization classes won't see the difference, and thus won't have to change.
You could also decide to hide to the user of the serialization classes where the data is stored. In that case the constructor of the serialization class creates the stream. This approach would be useful if you want all data to be serialized all on the same medium.