Object serialization to XML string  
Author Message
vasko





PostPosted: .NET Remoting and Runtime Serialization, Object serialization to XML string Top

Hi there,

Does anybody know how can I serialize an object to string that is XML representation of object.

So far I serialize object to XML file, get its content and store them in DB. I want to bypass XML file and get serialized object to string directly.

Thanks



.NET Development7  
 
 
Andrej Tozon





PostPosted: .NET Remoting and Runtime Serialization, Object serialization to XML string Top

Hi vasko,

you can use StringWriter class, like in the following example:

StringBuilder builder = new StringBuilder();
XmlSerializer s = new XmlSerializer(myObject.GetType());
using (StringWriter writer = new StringWriter(builder))
{
   
s.Serialize(writer, myObject);
}
string myXml = builder.ToString();

MyObject is the object you want to serialize...

Andrej



 
 
vasko





PostPosted: .NET Remoting and Runtime Serialization, Object serialization to XML string Top

Thanks Andrej,

Your code solve my problem. It was quick answer to.

Regards