Tim Huffam

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

  Home  |   Contact  |   Syndication    |   Login
  153 Posts | 0 Stories | 2747 Comments | 653 Trackbacks

News

Archives

Post Categories

Interesting Blogs/Links

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

Feedback

# re: Serialization: How to serialize and deserialize using C# .NET 6/15/2006 10:17 PM Lost but now found
Just what I was looking for!! Yippie!

# re: Serialization: How to serialize and deserialize using C# .NET 1/9/2008 11:40 AM Ridge Wong(China)
easy but power! many tks~

# re: Serialization: How to serialize and deserialize using C# .NET 1/31/2008 4:13 AM mstern
Very fine stuff!

# re: Serialization: How to serialize and deserialize using C# .NET 4/2/2008 7:52 PM ss
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?

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

# re: Serialization: How to serialize and deserialize using C# .NET 6/17/2008 4:23 AM pae
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?

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

# re: Serialization: How to serialize and deserialize using C# .NET 7/15/2008 3:49 AM xlthim
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

# re: Serialization: How to serialize and deserialize using C# .NET 5/7/2009 2:21 AM duheng
I want the code

# suggestion; NFileStorage to persisting multiple object to a single file 5/26/2009 6:51 PM Gert-Jan
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

# re: Serialization: How to serialize and deserialize using C# .NET 8/24/2009 11:15 AM ciamej
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);


# re: Serialization: How to serialize and deserialize using C# .NET 9/14/2009 1:21 PM CoderB
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();

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


# re: Serialization: How to serialize and deserialize using C# .NET 2/11/2010 3:22 PM XmlMax
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.

# re: Serialization: How to serialize and deserialize using C# .NET 6/9/2010 4:40 AM Anonymous
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;
}

# re: Serialization: How to serialize and deserialize using C# .NET 8/22/2010 9:50 PM AlwaysLearning
@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());
}

# re: Serialization: How to serialize and deserialize using C# .NET 2/22/2011 7:52 AM Rohith
The Code was very help full, Thanks a lot :)

# re: Serialization: How to serialize and deserialize using C# .NET 8/3/2011 5:53 AM KG
Thanks, stuff worked well...

# re: Serialization: How to serialize and deserialize using C# .NET 10/14/2011 4:14 AM Ken
Great post & very helpfull. Many Thanks!

# re: Serialization: How to serialize and deserialize using C# .NET 10/24/2011 4:12 AM Stoecki
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);

# re: Serialization: How to serialize and deserialize using C# .NET 1/24/2012 5:30 AM Sofia
Just what I was looking for!! Thannnks

# re: Serialization: How to serialize and deserialize using C# .NET 2/18/2012 5:10 PM Swamy
hey you saved my life, thanks a lot

Post A Comment
Title:
Name:
Email:
Comment:
Verification: