By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,132 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,132 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a


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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.