473,671 Members | 2,283 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:ShoppingL ist myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.8 9 ) );

// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xm l" );
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 65089
You can use a memory stream?

System.IO.Memor yStream ms1= new System.IO.Memor yStream();
s1.Serialize(ms 1, myList);

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I want to know how can i deserialize an object to a string, instead of a
file as below:ShoppingL ist myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.8 9 ) );

// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xm l" );
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******** ******@TK2MSFTN GP09.phx.gbl...
You can use a memory stream?

System.IO.Memor yStream ms1= new System.IO.Memor yStream();
s1.Serialize(ms 1, myList);

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I want to know how can i deserialize an object to a string, instead of a
file as below:ShoppingL ist myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.8 9 ) );

// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xm l" );
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******** *****@tk2msftng p13.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******** ******@TK2MSFTN GP09.phx.gbl...
You can use a memory stream?

System.IO.Memor yStream ms1= new System.IO.Memor yStream();
s1.Serialize(ms 1, myList);

"Mullin Yu" <mu*******@ctil .com> wrote in message
news:uP******** ******@TK2MSFTN GP12.phx.gbl...
I want to know how can i deserialize an object to a string, instead of a file as below:ShoppingL ist myList = new ShoppingList();
myList.AddItem( new Item( "eggs",1.49 ) );
myList.AddItem( new Item( "ground beef",3.69 ) );
myList.AddItem( new Item( "bread",0.8 9 ) );

// Serialization
XmlSerializer s = new XmlSerializer( typeof( ShoppingList ) );
TextWriter w = new StreamWriter( @"c:\list.xm l" );
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
7844
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 because the xml serilization in animalStr contains more than an Animal, it contains either Cat or Dog. Is there a neat way of dealing with deserilization of abstract types? ------------- Code Snippet ------------- abstract public class Animal {
0
2069
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 application implementation in another .dll (let's call that app.dll). app.dll makes a function call into another dll (let's call that one dep.dll). dep.dll has some code in it where it deserializes a memorystream using a binaryformatter.
4
3304
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) If Not IsNothing(filename) then fs = New System.IO.FileStream(filename, System.IO.FileMode.Open) Dim sf As New
8
6865
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 that string back into a font object? Thanks.
0
2064
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> <Size>
0
4080
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 using complex type structures declared in the WSDL file.
2
11532
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 working, but when I try to read the line back into a MemoryStream, and then to deserialize from that, I get an error that the root node doesn't exist. Example XML lines: <?xml version="1.0"?><LogItem
11
2810
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
2243
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(); formatter.Serialize(stream, this); }
0
8474
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8392
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8912
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8819
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7428
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5692
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2049
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.