Geeks With Blogs

Tim Huffam Dotting the I and crossing the T of I.T.

Serialize (convert an object instance to an XML document):

// Assuming obj is an instance of an object
XmlSerializer ser =
new
XmlSerializer(obj.GetType());
System.Text.StringBuilder sb = new
System.Text.StringBuilder();
System.IO.StringWriter writer = new
System.IO.StringWriter(sb);
ser.Serialize(writer, obj);
XmlDocument doc = new
XmlDocument();
doc.LoadXml(sb.ToString());

Deserialize (convert an XML document into an object instance):

//Assuming doc is an XML document containing a serialized object and objType is a System.Type set to the type of the object.
XmlNodeReader reader =
new
XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new
XmlSerializer(objType);
object
obj = ser.Deserialize(reader);
// Then you just need to cast obj into whatever type it is eg:
MyClass myObj = (MyClass)obj;

HTH
Tim

Posted on Thursday, February 9, 2006 2:59 PM BizTalk , C# .NET , ASP.NET , XML, XSL, etc | Back to top


Comments on this post: Serialization: How to serialize and deserialize using C# .NET

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Just what I was looking for!! Yippie!
Left by Lost but now found on Jun 15, 2006 10:17 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
easy but power! many tks~
Left by Ridge Wong(China) on Jan 09, 2008 11:40 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Very fine stuff!
Left by mstern on Jan 31, 2008 4:13 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Hi,

whether this code is working. When i put breakpoint in the following line, i could see that the value is none.

XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);

Why is it so?
Left by ss on Apr 02, 2008 7:52 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Works great! Thank you so much. It took me forever to find an example on how to do this!
Left by Anonymous on Apr 12, 2008 1:25 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
I implemented deserialization from an xml i read from disk but the deserialized object does not have the info the xml had, only the properties. Why?
Left by pae on Jun 17, 2008 4:23 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Is ok, was a nameSpaces issue, now is working fine, nice post, short, simple and very userful!!!
Left by pae on Jun 17, 2008 10:14 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Every sample I read is a great example of reading 1 object in with a few attributes. I need to read multiple objects in with multiple attributes... Like a collection of books, each with a title, isbn, author, price... How do I create a book object, add the attributes for that book, save it to a List<>, then read in the next book? tks xlthim
Left by xlthim on Jul 15, 2008 3:49 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
I want the code
Left by duheng on May 07, 2009 2:21 AM

# suggestion; NFileStorage to persisting multiple object to a single file
Requesting Gravatar...
Hi,

If you're interested in (de)serializing you might also want to take a look at NFileStorage; you can (de)serialize any object (or file, or string) to a file storage, see http://nfilestorage.codeplex.com/

Gert-Jan
Left by Gert-Jan on May 26, 2009 6:51 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
You can serialize in a much easier way (and more efficient):

// Assuming obj is an instance of an object
XmlDocument doc = new XmlDocument();
XPathNavigator nav = doc.CreateNavigator();
XmlWriter writer = nav.AppendChild();
XmlSerializer ser = new XmlSerializer(obj.GetType());
ser.Serialize(writer, obj);
Left by ciamej on Aug 24, 2009 11:15 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
ciamej's code works, but I had to add calls to flush the writer.

XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
XmlWriter writer = nav.AppendChild();

XmlSerializer ser = new XmlSerializer( obj.GetType() );
ser.Serialize( writer, obj );

writer.Flush();
writer.Close();
Left by CoderB on Sep 14, 2009 1:21 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
XmlSerializer ser = new XmlSerializer(objType);
can any one please explain what is the objType in the above sentence in de-serialization
Left by Mathi on Jan 25, 2010 10:34 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
XmlSerializer ser = new XmlSerializer(objType);

Serialization is strongly typed to a particular object which is serializable, so that .NET knows how to create the XML. So in order to serialize it, you need to instantiate the serializer with the type of object you are going to serialize.

Hope this helps.
Left by XmlMax on Feb 11, 2010 3:22 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
DeSerialization does not work properly can anybody tel what is the mistake


public XmlDocument SerializeObject(Object objToSerialize)
{
XmlSerializer ser = new XmlSerializer(objToSerialize.GetType());
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, objToSerialize);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
return doc;
}

public object DeSerializeAnObject(XmlDocument doc)
{
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
Object objType = new object();
XmlSerializer ser = new XmlSerializer(objType.GetType());
object obj = ser.Deserialize(reader);
return obj;
}
Left by Anonymous on Jun 09, 2010 4:40 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
@Anonymous:
>DeSerialization does not work properly can anybody tel what is the mistake

Yes. You're not passing the type of object you want to deserialize into your DeSerializeAnObject() method. You can't just use the "object" type like that.

You probably want to restructure it to be something like this:

public static XmlDocument SerializeObject(Object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
System.Text.StringBuilder sb = new System.Text.StringBuilder();
System.IO.StringWriter writer = new System.IO.StringWriter(sb);
ser.Serialize(writer, obj);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sb.ToString());
return doc;
}

public static object DeSerializeAnObject(XmlDocument doc, Type objType)
{
XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
XmlSerializer ser = new XmlSerializer(objType);
object obj = ser.Deserialize(reader);
return obj;
}

static void Main(string[] args)
{
//Test serialization...
StyleTemplate styleTemplate = new StyleTemplate();
styleTemplate.BorderColor = "Black";
styleTemplate.Color = "White";
styleTemplate.FontFamily = "Tahoma";
styleTemplate.FontSize = "10pt";
styleTemplate.Name = "Foo";

XmlDocument doc = SerializeObject(styleTemplate);
string xml = doc.InnerXml;
Console.WriteLine(xml);

//Test deserializtion...
XmlDocument doc2 = new XmlDocument();
doc2.LoadXml(xml);
StyleTemplate test = (StyleTemplate) DeSerializeAnObject(doc2, typeof (StyleTemplate));

//Set a breakpoint on the following to check the properties...
Console.WriteLine(test.ToString());
}
Left by AlwaysLearning on Aug 22, 2010 9:50 PM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
The Code was very help full, Thanks a lot :)
Left by Rohith on Feb 22, 2011 7:52 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Thanks, stuff worked well...
Left by KG on Aug 03, 2011 5:53 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Great post & very helpfull. Many Thanks!
Left by Ken on Oct 14, 2011 4:14 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
I have problems if the values of a given xml Element are doubles or floats. In both cases the deserializer forgets the floating point. has anyone an idea?

code:
XmlSerializer(typeof(myobj));
FileStream fs = new FileStream(xml.OuterXml, FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
myobj obj;
obj = (myobj)serializer.Deserialize(reader);
Left by Stoecki on Oct 24, 2011 4:12 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
Just what I was looking for!! Thannnks
Left by Sofia on Jan 24, 2012 5:30 AM

# re: Serialization: How to serialize and deserialize using C# .NET
Requesting Gravatar...
hey you saved my life, thanks a lot
Left by Swamy on Feb 18, 2012 5:10 PM

Your comment:
 (will show your gravatar)


Copyright © Tim Huffam | Powered by: GeeksWithBlogs.net | Join free