Just some sample code that uses an in-memory stream instead of System.IO.File. One thing to note is that DataContractSerializer saves as UTF8, not Unicode Encoding as some other XMLReader samples demonstrate.
If you see a way to alter the format ot the XML (compressed, or indented), please update this comment (or send me an email). Thanks!
namespace
serialTest
{
[
DataContract]
classmyTest
{
[
DataMember]
string First;
[
DataMember]
publicstring Last;
[
DataMember]
publicint age;
}
classProgram
{
staticvoid Main(string[] args)
{
myTest m = newmyTest { age = 59, Last = "Mankowski" };
System.IO.
MemoryStream someStream = new System.IO.MemoryStream();
DataContractSerializer dcs = newDataContractSerializer(typeof( myTest));
dcs.WriteObject(someStream, m);
someStream.Seek(0, System.IO.
SeekOrigin.Begin);
XmlDictionaryReader reader =
XmlDictionaryReader.CreateTextReader(someStream, newXmlDictionaryReaderQuotas());
// Read the remaining bytes, byte by byte.
someStream.Seek(0, System.IO.
SeekOrigin.Begin);
byte[] byteArray = newbyte[someStream.Length];
int count = someStream.Read(byteArray, 0, 20);
while (count < someStream.Length)
{
byteArray[count++] =
Convert.ToByte(someStream.ReadByte());
}
// Decode the byte array into a char array
// and write it to the console.
UTF8Encoding u8 = newUTF8Encoding();
char[] charArray = newchar[u8.GetCharCount( byteArray, 0, count)];
u8.GetDecoder().GetChars( byteArray, 0, count, charArray, 0);
Console.WriteLine(charArray);
Console.ReadLine();
}
}
}