470,565 Members | 1,792 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,565 developers. It's quick & easy.

C#: How to deserialize an object to a string

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();
Nov 12 '05 #1
3 64822
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();

Nov 12 '05 #2
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();


Nov 12 '05 #3
thanks!

got what i want! : >

cheers,
mullin

"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:eS*************@tk2msftngp13.phx.gbl...
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();



Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kristian Kjems | last post: by
reply views Thread by Fruber Malcome | last post: by
4 posts views Thread by George Addison | last post: by
8 posts views Thread by Frank Rizzo | last post: by
reply views Thread by John Manion via .NET 247 | last post: by
2 posts views Thread by Thomas S | last post: by
11 posts views Thread by wpmccormick | last post: by
reply views Thread by connectpalm03-forum | last post: by
1 post views Thread by livre | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.