473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 65065
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kristian Kjems | last post by:
Look at the Code Snippet below: It is not possible to Deserialize the Animal in animalStr directly because the type of the animal is not known. Deserializing into the abstract type will not work...
0
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
4
by: George Addison | last post by:
I understand this might not be the optimal method of deserialization, but how can I deserialize a class to itself? Something like: Public Sub New(Optional ByVal filename as string = Nothing)...
8
by: Frank Rizzo | last post by:
How do I serialize Font object into a string that I can store in either INI file or the registry (I know it’s a bad thing, but need to do it nevertheless). Then, also, how do I deserialize it from...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
0
by: Matt S | last post by:
Hello, I'm trying to build a C# client to consume an AXIS Web Service (running SOAP over HTTP). The Web Service encodes full server-side exception traces in the Soap Fault > Detail element...
2
by: Thomas S | last post by:
Any suggestions on how to deserialize an object from one line of XML? I'm trying to deserialize multiple objects from one XML document, each object on one line of the file. The serialization is...
11
by: wpmccormick | last post by:
I've a complex problem: I'm deserializing a very long string of XML into a very large object foo: <foo> ....... <bar>sometimes a simple string is here</bar> ....... </foo>
0
by: connectpalm03-forum | last post by:
I have a class named (MyClassA) in ControlClasses.dll and was able to serialize it to database. Like below SaveTo(MemoryStream stream) { IFormatter formatter = new BinaryFormatter(); ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.