In this article we will learn xml deserialization in c#. We have already learn how to work with xml serialization in c# in last article. The article explained what is Serialization? and Why do we need xml serialization? The article also covered few coding snippets/examples to c# serialize object in xml using c# xml serialization. If you are new to serialization or you need more information about xml serialization, then I would suggest you to refer xml serialization article.
|XML Deserialization in C#.Net|
What is De-Serialization?
From previous article we know that Serialization is the process of converting an object into a stream so that it can be saved in any physical file like (XML) or can be saved in Database. The main purpose of Serialization in C# is to persist an object and save it in any specified storage medium like stream, physical file or DataBase. Deserialization is vice-versa of Serialization.
Deserialization is the opposite process of serizalization in which the outcome of Serialization is again converted back into an original object.
What is XML DeSerialization?
The process of converting an XML into an object instance is known as XML Deserialization. To use deserialization in xml we need XmlSerializer class. This class is derived from System.Xml.Serialization. Two most important methods of this class are Serialize and Deserialize. These two methods are used to serialize object to xml and then again deserialize into object.
How to do XML DeSerialization in C#.Net?
So in this c# tutorial let’s learn how to deserialize xml into an object i.e. to read xml document and make an object from it. One thing we have learnt from XML serialization that – an object can be converted to xml schema and can be stored as a physical xml file. XML deserialization is a process in which we programmatically read that xml document and creates new object from that xml schema.
There are many ways c# serialize object to xml, so the xml Deserialization. This articles covers following ways of XML deserialization.
- DeSerialization of XML into simple class object : DeSerialize a simple XML into a simple object instance of a class.
- DeSerialization of XML containing many properties into a Class object : DeSerialize XML into an object instance of a class containing more properties.
- DeSerialization of XML containing XMLElement : We can control the name of properties using XMLElement in XML serialization. You can specify an alternate name for properties of an object to save into XML in serialization. Deserialize such XML into an object instance again.
- DeSerialization of XML into an array of Objects : DeSerialize XML of type arrays into the array of objects.
- DeSerialization of XML into a DataSet : DeSerialize a XML back into a DataSet.
Note: The examples of xml file used below are the outcome of XML serialization from previous article. Please refer XML serialization article to undesrtand how to generate below xml files.
A. DeSerialization of XML into simple class object
Consider below class –
Below xml schema shows how a single object of above class is serialized in xml file.
Hence, to deserialize above xml file you can use below DeSerialization code (method) –
B. DeSerialization of XML containing many properties into a Class object
For example you have below class –
XML serialization of above class will give you following xml –
C. DeSerialization of XML containing XMLElement
Suppose you have a class with xmlelement attributes like below –
And you have serialized xml of above class like below –
In this case also you can use DeSerializationCamera() given in example B.
D. DeSerialization of XML into an array of Objects
Again here we are taking the example of above camera class. However look at below xml file, it shows multiple camera (nodes) objects instead single camera node –
As above xml file includes multiple nodes (objects) of Camera types, to deserialize above xml file we need to modify above DeSerializationCamera() methods as follows –
The highlighted part shows how the original method updated to get the array of same objects from xml file.
E. DeSerialization of XML into a DataSet
To get an xml into DataSet you don’t need XMLSerializer class and it’s DeSerialize() method. For example if you have already serialized your DataSet into xml file like below xml file –
In this case you actually don’t need DeSerialize() method. Use following code to get above xml in Dataset.
I hope this article helped you to understand how to deserialize xml into an object. If yes then please feel free to share the article on your favourite social media using below “Sharing is Caring” widget.
More articles –