You can also use a StringWriter instead of a MemoryStream, since you know
you need a string object
StringWriter sw = new StringWriter();
s.Serialize( sw, myList );
string serializedXml = sw.ToString();
and a StringReader to deserialize:
newList = (ShoppingList)s.Deserialize( new StringReader( serializedXml ) );
--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor
"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message
news:#F**************@TK2MSFTNGP09.phx.gbl...
You can use a memory stream?
System.IO.MemoryStream ms1= new System.IO.MemoryStream();
s1.Serialize(ms1, myList);
"Mullin Yu" <mu*******@ctil.com> wrote in message
news:uP**************@TK2MSFTNGP12.phx.gbl... I want to know how can i deserialize an object to a string, instead of a
file as below:ShoppingList myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.89 ) );
// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xml" );
s.Serialize( w, myList );
w.Close();
// Deserialization
ShoppingList newList;
TextReader r = new StreamReader( "list.xml" );
newList = (ShoppingList)s.Deserialize( r );
r.Close();