2. What are we going to learn today?
What is Xml Serialization, why to use it?
XmlSerializer Class
Serialize() method
Demo – Different types of serializations
Basic attributes to control serialization process
Xsd.exe
Some common troubleshooting
Exception handling
Presenter: Priyojit Mondal, Mindfire Solutions
3. What is Serialization and why do we
need it?
To persist an object and save it in a specified storage location like a
physical file or Database.
Serialization converts an object into a stream and then uses it
either to save it in a file or to send/transport over any
communication channel.
Presenter: Priyojit Mondal, Mindfire Solutions
4. What is XML serialization in C#.Net?
A serialization technique in which an object will be
converted/saved into xml format or stream.
To use serialization in xml we need XmlSerializer class. This class is
derived from System.Xml.Serialization.
Serialize and Deserialize are the two most important methods.
Presenter: Priyojit Mondal, Mindfire Solutions
5. Things to remember...
Only public objects can be serialized in XML serialization. And
hence it is also called as Shallow serialization.
Classes inherited from IEnumerable and ICollection can also be
serialized, however it serializes only collections and not public
properties.
Private or read-only properties, Methods, Indexers cannot be
serialized using this xml serialization.
Presenter: Priyojit Mondal, Mindfire Solutions
6. XmlSerializer Class
The central class in XML serialization
Namespace: System.Xml.Serialization
Assembly: System.Xml (in System.Xml.dll)
Serializes and deserializes objects into and from XML documents.
The XmlSerializer enables you to control how objects are encoded
into XML.
MSDN Link
Presenter: Priyojit Mondal, Mindfire Solutions
7. How to do XML Serialization?
XML Serialization of simple class object : Serialize the object into
XML as it is.
XML Serialization of a Class object containing many properties :
Serialize the object into XML as it is.
XML Serialization using XmlElement : 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.
XML Serialization of array of Objects : Serialize the array of objects
into XML.
Presenter: Priyojit Mondal, Mindfire Solutions
10. XmlSerializerNamespaces Class
Contains the XML namespaces and prefixes that the XmlSerializer
uses to generate qualified names in an XML-document instance.
MSDN Link
Presenter: Priyojit Mondal, Mindfire Solutions
11. XML Schema Definition Tool (Xsd.exe)
The XML Schema Definition (Xsd.exe) tool generates XML schema
or common language runtime classes from XDR, XML, and XSD files,
or from classes in a runtime assembly.
Format :
xsd [file_path] /c[lasss] /o[utput]:[directory]
MSDN Link
Presenter: Priyojit Mondal, Mindfire Solutions
13. Exceptions from the XmlSerializer
In most cases Serialize(), Deserialize() and even the XmlSerializer
constructor throws generic System.InvalidOperationException when an
error occurs.
Message : “There was an error generating the XML document”
The Serialize() method catches all exceptions thrown in the serialization
classes, wraps them in an InvalidOperationException, and throws that up
the stack.
To get the real exception use InnerException or exception's ToString()
method.
Presenter: Priyojit Mondal, Mindfire Solutions