it seminar-xml serialization

15
Introduction to Xml Serialization in C# Presenter: Priyojit Mondal, Mindfire Solutions Date: 20/12/2013

Upload: priyojit-mondal

Post on 10-May-2015

138 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: It seminar-xml serialization

Introduction toXml Serialization in C#

Presenter: Priyojit Mondal, Mindfire SolutionsDate: 20/12/2013

Page 2: It seminar-xml serialization

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

Page 3: It seminar-xml serialization

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.

Page 4: It seminar-xml serialization

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

Page 5: It seminar-xml serialization

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

Page 6: It seminar-xml serialization

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

Page 7: It seminar-xml serialization

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

Page 8: It seminar-xml serialization

Demo

Presenter: Priyojit Mondal, Mindfire Solutions

Page 9: It seminar-xml serialization

Customization XmlRoot XmlElement XmlAttribute XmlText XmlEnum

Presenter: Priyojit Mondal, Mindfire Solutions

Page 10: It seminar-xml serialization

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

Page 11: It seminar-xml serialization

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

Page 12: It seminar-xml serialization

Troubleshooting

Case 1: Declaring Serialization Types.

Case 2: Working with a class with parametrized constructor.

Presenter: Priyojit Mondal, Mindfire Solutions

Page 13: It seminar-xml serialization

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

Page 14: It seminar-xml serialization

Presenter: Priyojit Mondal, Mindfire Solutions

Question and Answer

Page 15: It seminar-xml serialization

Presenter: Priyojit Mondal, Mindfire Solutions

Thank you