it seminar-xml serialization
TRANSCRIPT
Introduction toXml Serialization in C#
Presenter: Priyojit Mondal, Mindfire SolutionsDate: 20/12/2013
Presenter: Priyojit Mondal, Mindfire Solutions
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.
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.
Presenter: Priyojit Mondal, Mindfire Solutions
MSDN Link
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]
Presenter: Priyojit Mondal, Mindfire Solutions
MSDN Link
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
Presenter: Priyojit Mondal, Mindfire Solutions
Question and Answer
Presenter: Priyojit Mondal, Mindfire Solutions
Thank you