Introduction to
Xml Serialization in C#

Presenter: Priyojit Mondal, Mindfire Solutions
Date: 20/12/2013
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
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
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
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
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
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
Demo

Presenter: Priyojit Mondal, Mindfire Solutions
Customization






XmlRoot
XmlElement
XmlAttribute
XmlText
XmlEnum

Presenter: Priyojit Mondal, Mindfire Solutions
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
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
Troubleshooting
Case 1:
Declaring Serialization Types.
Case 2:
Working with a class with parametrized constructor.

Presenter: Priyojit Mondal, Mindfire Solutions
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
Question and
Answer

Presenter: Priyojit Mondal, Mindfire Solutions
Thank you

Presenter: Priyojit Mondal, Mindfire Solutions

It seminar-xml serialization

  • 1.
    Introduction to Xml Serializationin C# Presenter: Priyojit Mondal, Mindfire Solutions Date: 20/12/2013
  • 2.
    What are wegoing 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 Serializationand 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 XMLserialization 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...  Onlypublic 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 centralclass 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 doXML 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
  • 8.
  • 9.
  • 10.
    XmlSerializerNamespaces Class  Contains theXML 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 DefinitionTool (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
  • 12.
    Troubleshooting Case 1: Declaring SerializationTypes. Case 2: Working with a class with parametrized constructor. Presenter: Priyojit Mondal, Mindfire Solutions
  • 13.
    Exceptions from theXmlSerializer  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
  • 14.
    Question and Answer Presenter: PriyojitMondal, Mindfire Solutions
  • 15.
    Thank you Presenter: PriyojitMondal, Mindfire Solutions