A long while ago we posted a tutorial on how to serialize objects to a binary file. While this is very useful, unfortunately the resulting file is not very human readable. In this tutorial, I'm going to demonstrate how to serialize your own objects to and from an XML file.
Since .NET can use reflection to get property names, basic serialization is unbelievably simple. It only gets slightly difficult when you want to name your XML tags differently than your property names (but still not very hard). If you've ever used an XML serialization package in C++ like boost, tinyXML, or libXML2, you'll see how comparatively easy C# is to use.
Let's start with a basic example. Below is an object that stores some information about a movie.
public class Movie
{
public string Title
{ get; set; }
public int Rating
{ get; set; }
public DateTime ReleaseDate
{ get; set; }
}
All right, now that we have an object, let's write a function that will save it to XML.
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
The first thing I do is create an XMLSerializer (located in the System.Xml.Serialization namespace) that will serialize objects of type Movie. The XMLSerializer will serialize objects to a stream, so we'll have to create one of those next. In this case, I want to serialize it to a file, so I create a TextWriter. I then simply call Serialize on the XMLSerializer passing in the stream (textWriter) and the object (movie). Lastly I close the TextWriter because you should always close opened files. That's it! Let's create a movie object and see how this is used.
static void Main(string[] args)
{
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
SerializeToXML(movie);
}
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
After this code executes, we'll have an XML file with the contents of our movie object.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Title>Starship Troopers</Title>
<Rating>6.9</Rating>
<ReleaseDate>1997-11-07T00:00:00</ReleaseDate>
</Movie>
If you noticed, all of the XML tag names are the same as the property names. If we want to change those, we can simply add an attribute above each property that sets the tag name.
public class Movie
{
[XmlElement("MovieName")]
public string Title
{ get; set; }
[XmlElement("MovieRating")]
public float Rating
{ get; set; }
[XmlElement("MovieReleaseDate")]
public DateTime ReleaseDate
{ get; set; }
}
Now when the same code is executed again, we get our custom tag names.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MovieName>Starship Troopers</MovieName>
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
Sometimes, in XML, you want information stored as an attribute of another tag instead of a tag by itself. This can be easily accomplished with another property attribute.
public class Movie
{
[XmlAttribute("MovieName")]
public string Title
{ get; set; }
[XmlElement("MovieRating")]
public float Rating
{ get; set; }
[XmlElement("MovieReleaseDate")]
public DateTime ReleaseDate
{ get; set; }
}
With this code, MovieName will now be an attribute on the Movie tag.
<?xml version="1.0" encoding="utf-8"?>
<Movie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
MovieName="Starship Troopers">
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
Let's move on to something a little more interesting. Let's create another movie and serialize a List of them to our XML file. Here's the modified code to do just that:
static void Main(string[] args)
{
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
Movie movie2 = new Movie();
movie2.Title = "Ace Ventura: When Nature Calls";
movie2.ReleaseDate = DateTime.Parse("11/10/1995");
movie2.Rating = 5.4f;
List<Movie> movies = new List<Movie>() { movie, movie2 };
SerializeToXML(movies);
}
static public void SerializeToXML(List<Movie> movies)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Movie>));
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, movies);
textWriter.Close();
}
Now we have XML that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfMovie xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Movie MovieName="Starship Troopers">
<MovieRating>6.9</MovieRating>
<MovieReleaseDate>1997-11-07T00:00:00</MovieReleaseDate>
</Movie>
<Movie MovieName="Ace Ventura: When Nature Calls">
<MovieRating>5.4</MovieRating>
<MovieReleaseDate>1995-11-10T00:00:00</MovieReleaseDate>
</Movie>
</ArrayOfMovie>
Ok, so you can see how easy it is to get your objects into an XML document. Let's now look at how to read an XML document back into our objects - deserialization. The process of deserializing is very similar to what we did for serialization.
static List<Movie> DeserializeFromXML()
{
XmlSerializer deserializer = new XmlSerializer(typeof(List<Movie>));
TextReader textReader = new StreamReader(@"C:\movie.xml");
List<Movie> movies;
movies = (List<Movie>)deserializer.Deserialize(textReader);
textReader.Close();
return movies;
}
Just like before, we first create an XmlSerializer that can deserialize
objects of type List\. The XmlSerializer also deserializes from
a stream, so we create a file stream from our XML file. We then simply
call Deserialize on the stream and cast the output to our desired
type. Now the movies List is populated with objects that we previously
serialized to the XML file.
The deserializer is very good at handling missing pieces of information in your XML file. Let's say the second movie didn't have the MovieName attribute on the Movie tag. When the XML file is deserialized, it simply populates that field with null. If MovieRating wasn't there, you'd receive 0. Since a DateTime object can't be null, if MovieReleaseDate was missing, you'd receive DateTime.MinValue (1/1/0001 12:00:00AM).
If the XML document contains invalid syntax, like say the first opening Movie tag was missing, the Deserialize call will fail with an InvalidOperationException. It will also be kind enough to specify the location in the file where it encountered the error (line number, column number).
One thing to remember is that the basic XML serialization won't maintain references. Let's say I populated my movies list with the same movie reference multiple times:
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = 6.9f;
List<Movie> movies = new List<Movie>() { movie, movie };
Now I have a list containing two of the exact same movie reference. When I serialize and deserialize this list, it will be converted to two separate instances of the movie object - they would just have the same information. Along this same line, the XMLSerializer also doesn't support circular references. If you need this kind of flexibility, you should consider binary serialization.
There's still a lot to cover when it comes to XML serialization, but I think this tutorial covers enough of the basics to get things rolling.
Thank you very much! Very clear.
TextWriter implements IDisposable; this code would be safer if it used the using statement.
You're absolutely right. I'll modify the code examples to use the 'using' statement. That also would eliminate the need for the Close() call since the TextWriter closes itself when it's disposed.
I am doubt that it is possible deserilize the XML file without doing the serilize object? That means if I have a XML file created manually and then I just want to get the object information in the project(read only), is it possible? And how?
The deserializer does not need XML that was created by the serializer. You can use any manually created XML document, just as long as the elements have matching property names.
Thank you very much. As you said if the elements match the property names, I can deserialize the XML document. So this means I cannot use the deserializer in .NET like XmlSerializer.Deserializer()? Can you give me an example to clarify it?
Let's say I have a hand written XML document like this:
And I have a C# object like this:
I can deserialize that XML document into my object using the following code:
Since the XML document has the correct root node and has tags that match property names of my object, everything will work fine. If I changed the root node of my XML document to something like this:
the deserialization won't work, because it will expect the root node to be named "MyObject". However, you can still make this work by modifying the object to include what the root node is supposed to be named:
Now we're saying that even though the object is named "MyObject", it will be called "SomeOtherObject" inside the XML document. And again, everything will work.
Hope this clears stuff up.
Well.. And if i want
6,9 07-11-1997
?? (Custom DateTime and Flot number format)..
Your really don't need it. Serialization is the process used for tranforming data to universaly readable and updatable format. You don't need it in custom format, because on the other side of stream is application which can process DateTime to correct ouput.
This explanation has been very helpful and is nice and clear, thanks!
However, a complete code listing at the bottom with all the usings included, and where the code blocks should be placed in either a single file or in Visual Studio, would be even more helpful to us newbies.
thanks for the great article.
in your example above, if i want to set attribute for element name lets say Rating, how can i do that? or do i have to create Rating as Class and apply attribute there? i think i can do that way but creating new class just to fulfill that doesn't seem quite right.
any suggestion?
cheers
Can I do deserialize and deserialize specific node not entrie XML doc? for example I want to deserialzie only data in below xml file. Can it be done?
20
20
Sorry Ramki, but the comment system strips \< and >. Refer to this post on how to put xml and other code in the comments.
This is a great tutorial I found.
Thanks a lot to provide this kind of clear tutorial.
dear sir Request you to solve my issue.i.e. Let me know how to use jquery inside a xml file.if iam using so its giving err0r as 1.\$ is not defined 2.script is not defined
Is there any way to serialize an object that has private or internal set accessors, e.g:
public propertyname {private set; get; }
I can't seem to get it to work.
Strangley, serializing a VB class that has readonly properties works if you impliment ISerializable and use reflection to skip over your readonly properties, but it doesn't seem to play ball in c#.
Thank for the important article. i'm trying to do serilization with files that are writing to an isolate storage - without success. do you have an idea why??
Extremely helpful, Reddest. Always love your blog for the best and most useful information - keep up the good work!
Excellent tutorial! I look forward to reading more.
Saved lot of my time.Easy to follow. Keep this great works coming.
im trying to do almost the same thing eccapt from that i want to store the stream in a string on a hidden field on the page but it seems that the deserialization dosnt work if i put it on a string (if ichange the source to a file it works like charm) any idieas ?
Thanks a lot. good article to beign with. tried the examples and really good.
Thanks a lot for the example I want to ask you how to serialize a complex object, I mean an object with some generic list property type. I have always an invalid operation exception or error when creating the xml document or a reflection error.
structure sample:
object1 prop1 string prop2 int prop3 List
object2 prop1 string prop2 string prop3 List
Lists have XML serialization support built right in. The XML serializer doesn't support circular references, so if the List in object1::prop3 contains a reference to object1, you'll get an error. That's all I've got for now. If you post some example code, I can probably help more.
Maybe xml serialization doesn't work on mixed type lists. I think it may only work on typed lists, like List.
Nice article. But i'm stuck on one issue where i want to write comment in between the node. How can i achieve that using serialization?
For e.g. I want the output like:
This was fantastic information, thank you. Now I have another question. What if what the document I want to read from XML has the following structure in one of its subtrees? how should I create the object?
Would using a list or even a Dictionary work?
Thanks in advance.
that xml does not conform to a relational model and is not recommended for serialization. in your xml, the name & value elements are required to have a particular order of existence, otherise the value of one may get mixed up with some other name.
in xml standards, design like this creates ambiguity and hard to use for programming APIs.
Good information, thank you for commenting. I've had a similar instance where I wanted to added different elements, but definitely wanted to remove the ambiguity. As it turns out, I modified my XML to use attributes to absorb the names/values.
And thanks for the suggestion about the XmlArray. I'm looking to utilize that suggestion in a project.
How would you serialize and deserialize this?
I don't think deserialization can handle this case by default. You can override the XML serializer for an object, however I don't know the syntax off the top of my head.
XmlArray attribute could be utilized.
try the following: public item[] foods = new item[3]; foods[0] = new item(); foods[1] = new item(); foods[2] = new item(); XmlSerializer serializer = new XmlSerializer(typeof(item[]));
But in that case I have XML like this:
Does anyone knows how can I rename ?
Sorry, there is xml:
Thanks for the simple explanation of serialization
Nice article, but I have one question.
I understand everything up to the point where you write the List to the xml file. I understand how to create a List and how to add elements to the List and even how to write the List to the xml file. Your example though shows a root element of ArrayOfMovie and then Movie elements inside that. I cannot see how you create the ArrayOfMovie. When I do it, I get a bunch of Movie elements and an error about more than one root element. I have looked over the code several times, but I do not see how you did that. Could you explain in more detail or provide a file that I could download to look at?
Great article!
If I have:
And I want to serialize the Movie like this:
How do I do that? That is, how do I tell the Movie class to only serialize the Name property of the LeadActor member instead of the whole object?
Thanks!
Well first your class fields have to be public (But I'm sure you already knew that)
Then use these Xml Attributes on your Actor class:
And you are good to go!
Well stanbeard, that's the question i've been looking for the answer all day long.
See my response to Stanbeards question
DeSerialize does not work can anybody help.I have very little knowledge about this
What doesn't work? Are you getting an exception? Is your file empty? Details please.
No Its not empty.. I get an Exception.
Invalid Operation Exception "There is an error in XML document (2, 2)."
Inner Exception caught is {" was not expected."}
No Its not empty.. I get an Exception.
Invalid Operation Exception "There is an error in XML document (2, 2)."
Inner Exception caught is TaskListDO xmlns='' was not expected."
I am Trying to Serialize an object in my Web application(Serializing works) and when i try to Deserialize the same string in WebServices it gives the exception..and one more Doubt is in reference.cs the class which has to be serialized has to be autogenerated is it.. if yes its not generated in my reference.cs file
Hello,
I dint make any changes but I am getting the class which has to be serialized in reference.cs I mean it is autogenerated... But now my problem is after deserializing the XML string I am not able to get the values passed from my application...
Please Help
Doesn't look too difficult.
In your deserializer function you try to use an object of type Object. In this case the serializer doesnt have a clue of which type of object you really want to deserialize. Since Object doesnt have any public properties it's likely that it breaks. You need to pass in the real Type of the object you want to deserialize: XmlSerializer(MyType)
Thank you very..... much for an excellent article. This is very clear. I read lots of stuff related to xmlserializer but nothing else was this clear. Keep up the good work.
Hi, Thanks allot, that was extremely wonderful :-)
but the code does not run for me!
Ah I found out that "class Class1" must be "public class Class1"
how to de-serialize it ?
I have problems in serializing a list. Here is my class:
Here is how I call the code:
When I run the code I get serialized XML like this: [language] Episode Begin Datetime 04/01/2010 Episode End Datetime 04/02/2010 [/langauge] There is slight problem. I have to rid of the tag because the receiving program does not validate the tag. IMy sericalzed XMl should look like this: [language] Episode Begin Datetime 04/01/2010 Episode End Datetime 04/02/2010 [/langauge] Can you tell me how to this problem solved?
The later part of my posting got garbled. Sorry. I am repostng only that part. The serialized output looks like this: [language] Episode Begin Datetime 04/01/2010 Episode End Datetime 04/02/2010 [/language]
There is slight problem. I have to get rid of the tag because the receiving program does not validate the tag. My sericalzed XML should look like this: [language] ProblemType> Episode Begin Datetime 04/01/2010 Episode End Datetime 04/02/2010 [/language] Can you please tell me how to get this problem solved?
hi i m wondering if i have an xml file like this
can i deserialaze it using the same method thanks for your help
Hi,
This is truly very nice and easy article. Thanx. Keep up the good work.
Hi,
How will the code change for a more complicated XML like the following:
I need to be able to represent a user from this, with the attributes USER_ID, LOGIN, FIRST_NAME, LAST_NAME, DELETED and IS_ADMIN.
I have followed the steps detailed above and came up with the following class:
But it will still not compile - it is complaining that " Can you please be kind and advise on this?
why do you have the "r" ? nsawnan1
If you are declaring XmlRoot= "resultset" the Reader will see the "table" XML Element, not "" XMLnode.
would it hurt you to post correct code instead of just asking a question?
When I serialize a bool array I get an output similar to:
How do I get it to look like:
I want to write an attribute and value at the same time.
Thanks, Geoff
Apologies if this didn't post in xml format. I won't "crud" up the page again.
Freaking AWESOME example thanx!
Very useful. Thanks.
Hi I can't seem to resolved this issue
System.InvalidOperationException was unhandled Message=There is an error in XML document (1, 2). Source=System.Xml StackTrace: at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) at ConsoleApplication1.Program.DeserializeFromXML3(String xml) in C:\WPF in Two Weeks\ConsoleApplication1\ConsoleApplication1\Program.cs:line 138 at ConsoleApplication1.Program.Main(String[] args) in C:\WPF in Two Weeks\ConsoleApplication1\ConsoleApplication1\Program.cs:line 21 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException: System.InvalidOperationException Message= was not expected. Source=wzmj2ehc StackTrace: at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderList1.Read3_ArrayOfMovies() InnerException:
[Serializable] [XmlRoot("Movies")] public class Movies { [XmlElement("ID")] public int ID { get; set; }
[XmlElement("Title")] public string Title { get; set; }
[XmlElement("ReleaseDate")] public DateTime ReleaseDate { get; set; }
[XmlElement("Genre")] public string Genre { get; set; }
[XmlElement("Price")] public decimal Price { get; set; }
}
this is my xml fragment.
Thanks so much
Thanks a lot for this post - very clear and very helpful!
Hi! I have a question:
Let's say, you have about one hundred entries(movies) in your collection, and you do not want to overwrite the file, every time you add an entry.
What would you do, if you wanted to just add another movie to your xml file?
Trying to deserialize a single movie into an existing file via xmldeserializer will result in an error message, as it is only made for writing a new xml file including version info etc.
Here is my code:
This is the error message I have been talking about: {"WriteStartDocument cannot be called on writers created with ConformanceLevel.Fragment."}
Thank you very much
XML is not designed to be used for random access like this. Serializing and deserializing 100 movie objects will be trivially fast anyway. If you want to insert individual items without rewriting the file, you should start looking into databases.
Nice tutorial, it was really interesting. Also there a simple tutorial on XML :-
http://zeeshanumardotnet.blogspot.com/2011/05/xml-serilization-tutorial.html
Hey, Is there something special you have to do to get this code to work with service contracts?
Here's some example code:
This code serialized the xml but leaves ReleaseDate as ReleaseDate rather than MovieReleaseDate.
Thank you.
I figured it out. For those who are interested, you just need to add an attribute to the "GetMovie" function called [XmlSerializerFormat].
This causes it to use the XML Serializer rather than the OperationalContract one.
Excellent for a fresher to learn... Thank u.
Thank you for the clear explanation.
This is a fantastic article. you have saved me a bunch. It's simple and complete. Thanks so much
xyz Male can any one help me how could i update the name and gender after deserlization the xml.
xyz Male
can any one help me how could i update the name and gender after deserlization the xml.
Hey did you get, how to update value in xml file? Thanks
how could i update the satus with new status or any other. after deserlization the xml.
Thanks!
Thanks for the post
My XMl file has a namespace. How can I deserealize. How can I deserealize with a namespace in my XML
Unhandled Exception: System.InvalidOperationException: There is an error in XML document (5, 2). ---> System.InvalidOperationException: was not expected.
Thanks for taking the time to put this up! So valuable!
Thanks for the post!
Nice
can i serialize object that is recursive ?
hello can any body say how to serialize object that contain self reference ?
Thanks a lot. I have learned what exactly Serialization is.
Starship Troopers rating ought to be 1.9. :)
Very good example for XML serialization. Thanks a lot.
Real good and simple example thx a lot
This article gave me a much clear picture regarding serialization.Thanks a lot!
Very, very informative - thank you ! I'd suggest also adding reasonably clean example of common problem - how to remove namespace reference from RootElement, such as found here:
http://www.diaryofaninja.com/blog/2011/07/19/xmlserializer--removing-namespace-amp-schema-declarations-xmlnsxsi-xmlxsd
On some occassions (e.g. import to TestLink) this information isn't necessary, and even distrubs.
Good, Very Good Job. Thanks!!!!!! :)
Thank You:)
The example works, thanks a lot!
Thanks, this was very helpful!
thanks bro..good job
Thank you!
HI! I want to seilize a pdfConverter object to xml file. i get an error: "To be XML serializable, types which inherit from ICollection must have an implementation of Add(System.String) at all levels of their inheritance hierarchy. System.Collections.Specialized.NameValueCollection does not implement Add(System.String)".
Is there any way to serilze the pdfConvereter object?
thank you very very much for your helpful article :)
very good
Good! It's Exectly What Iv'e Searched! Thank you very much!
hi sir, very thanking you sir, this is very use full to me, and am also working some company, so i requesting to u please forward total material regarding customizing our xml format material to my mail id: vamsi.r11@gmail.com
I am new to C# and trying to understand the deserializer Example below for me is missing something.. i wanted to have one program create the XML, which it does, but then I wanted a separate program to deserialize the XML, but it always complains. "Error 1 } expected c:\users\mike.keeney-robinson\documents\visual studio 2010\Projects\XML008\XML008\Program.cs 28 10 XML008 "
[language] namespace XML008 { class Program { public class Movie { public string Title { get; set; }
public int Rating { get; set; }
public DateTime ReleaseDate { get; set; } } static void Main(string[] args) { static List DeserializeFromXML() { XmlSerializer deserializer = new XmlSerializer(typeof(List)); TextReader textReader = new StreamReader(@"C:\movie.xml"); List movies; movies = (List)deserializer.Deserialize(textReader); textReader.Close(); return movies; } } } } [/language]
easier to read now::
i wanted to have one program create the XML, which it does, but then I wanted a separate program to deserialize the XML, but it always complains. "Error 1 } expected c:\users\mike\documents\visual studio 2010\Projects\XML008\XML008\Program.cs 28 10 XML008 "
Thank you so much for making a clear and simple XML tutorial. This is exactly what I needed.
Great tutorial, really helped alot.
To learn more on XML Serialization and Deserialization visit this links.
XML Serialization : http://jayeshsorathia.blogspot.com/2012/08/net-tips-c-tips-xml-serialization-of-custom-class-objects.html
XML Deserialization : http://jayeshsorathia.blogspot.com/2012/08/net-tips-c-tips-xml-deserialization.html
These links contains samples to download.
Very nice, clear and helpful article. Thank you very much and keep the good work continue. May god give you a great success in life.
Very Helpful! Thank you very much, helped a lot!
Thanks for a great article
An awesome article...thank you very much ...
Thank you for all your great articles. You're making a difference!
Bravo. Yes, indeed - an excellent article. Highly useful, rare excellent comprehensible writing style, and just brilliant logic. I can actually use everything here without going off to some other search to understand what's presented. Thanks! Cheers.