XML Deserialize

  • Thread starter Thread starter Mullin Yu
  • Start date Start date
M

Mullin Yu

i want to deserialize a object to a xml string. what i currently have is
deserialize it to a xml file.

i don't want to have one more step to load it back!!

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();
 
i want to deserialize a object to a xml string. what i currently have is
deserialize it to a xml file.

i don't want to have one more step to load it back!!

Serialize to a MemoryStream. then read the string from the memory
stream ('writeStream' is a memory stream here)

string result = System.Text.Encoding.UTF8.GetString(writeStream.ToArray());

FB
 
Back
Top