472,989 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How do I serialize an object to a string instead of a stream?

This is the example from MSDN where an object is serialized to a
filestream:

MySerializableClass myObject = new MySerializableClass();
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new
XmlSerializer(typeof(MySerializableClass));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, myObject);

How do I make a similar code that would give me a string or an XML
Document that contains the XML-serialized object?
Nov 12 '05 #1
3 52143


Kristian Kjems wrote:
This is the example from MSDN where an object is serialized to a
filestream:

MySerializableClass myObject = new MySerializableClass();
// Insert code to set properties and fields of the object.
XmlSerializer mySerializer = new
XmlSerializer(typeof(MySerializableClass));
// To write to a file, create a StreamWriter object.
StreamWriter myWriter = new StreamWriter("myFileName.xml");
mySerializer.Serialize(myWriter, myObject);

How do I make a similar code that would give me a string
You should be able to serialize to a StringWriter instead of a StreamWriter:

Person p1 = new Person();
p1.Name = "Kibo";

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));

StringWriter stringWriter = new StringWriter();

xmlSerializer.Serialize(stringWriter, p1);

string serializedXML = stringWriter.ToString();

Console.WriteLine(serializedXML);

or an XML
Document that contains the XML-serialized object?


That should work too by serializing to a memory stream and loading the
XmlDocument from that:

Person p1 = new Person();
p1.Name = "Kibo";

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Person));

MemoryStream memStream = new MemoryStream();

StreamWriter streamWriter = new StreamWriter(memStream);
xmlSerializer.Serialize(streamWriter, p1);

memStream.Position = 0;

StreamReader streamReader = new StreamReader(memStream);

XmlDocument serializedXML = new XmlDocument();

serializedXML.Load(streamReader);

Console.WriteLine(serializedXML.OuterXml);
--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 12 '05 #2
Cheers mate, was exactly the kind of information I was looking for.
Had been searching for the C++ stringstream equivalent in C#, but did not
know it was called StringWriter.

Nov 12 '05 #3
With the information given to me in this thread I made an abstract class
that if inherited from will be able to return the current object as an XML
serialized string:
The "get" is working, but the last line is "set" is not.
I get these two not surprisingly error messages from VS.Net:
"Cannot assign to '<this>' because it is read-only"
"Cannot implicitly convert type 'object' to 'AbstractXMLObject'"

Is there any nifty way, the fundamental idea of "set" can be possible
anyway or does it strive against fundamental principles?

abstract public class AbstractXMLObject
{
public string XML
{
get
{
XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter,this);
return stringWriter.ToString();
}
set
{
XmlSerializer xmlSerializer = new XmlSerializer(this.GetType());
StringReader stringReader = new StringReader(value);
this = xmlSerializer.Deserialize(stringReader);
}
}
}

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: films | last post by:
I understand the concept. Serialization of a class will add all the sub-objects of the class to the stream if there are also serializible. So say I have: class Author {
2
by: nick | last post by:
Is it recommended to serialize a custom object when putting it in session? I have a User object with properties ID, FirstName, LastName, and an array of Permissions which I would like to keep in...
2
by: Marcel Balcarek | last post by:
Does anyone have an example of serializing an object to a database table?
6
by: Hayato Iriumi | last post by:
I'm looking for a way to convert String to Stream. I don't want to have to write the string out to a file to get the stream. Does anyone know how? TIA
3
by: MMiGG | last post by:
Hi Our project need parse JAVA serialized object string in C, has any library? Thanx
4
by: Evan Camilleri | last post by:
How can I trasform (as fast as possible) an object to a binary memory stream? Evan
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Hi Is there any way to Read an INIFile from a string or Stream instead of a physical file ??? I want to read the INIFile into a string then store in a db but when I read the string from the...
1
by: pavankumar106 | last post by:
hi can anyone tell me how to convert a string to stream?
5
by: labmonkey111 | last post by:
I have a table in SQLServer with a varchar field set to NOT NULL, with a default value of the empty string. This table is linked in access, and the field is linked to a text box on a form. When I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.