This must be very simple but I can’t find it by searching.
I have the following code to serialize an object to a file and back. But now I want to serialize to a byte[] and back.
XmlSerializer serializer = new XmlSerializer(typeof(Class1));
using (TextWriter textWriter = new StreamWriter(path))
serializer.Serialize(textWriter, class1);
using (TextReader textReader = new StreamReader(path))
class1b = (Class1)serializer.Deserialize(textReader);
I tried using a MemoryStream:
byte[] buffer = new byte[1000];
using (TextWriter textWriter = new MemoryStream(buffer))
...
but I get an error. So how should I do it?
You should send the stream to the StreamWriter instead of trying to assign the Stream to a TextWriter.