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...
What is Serialization and why do we
need it?
To persist an object and save it in a specified storage location like a
physi...
What is XML serialization in C#.Net?


A serialization technique in which an object will be
converted/saved into xml form...
Things to remember...


Only public objects can be serialized in XML serialization. And
hence it is also called as Shallo...
XmlSerializer Class


The central class in XML serialization




Namespace: System.Xml.Serialization
Assembly: System.X...
How to do XML Serialization?


XML Serialization of simple class object : Serialize the object into
XML as it is.



XML...
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 qualifie...
XML Schema Definition Tool (Xsd.exe)


The XML Schema Definition (Xsd.exe) tool generates XML schema
or common language r...
Troubleshooting
Case 1:
Declaring Serialization Types.
Case 2:
Working with a class with parametrized constructor.

Presen...
Exceptions from the XmlSerializer


In most cases Serialize(), Deserialize() and even the XmlSerializer
constructor throw...
Question and
Answer

Presenter: Priyojit Mondal, Mindfire Solutions
Thank you

Presenter: Priyojit Mondal, Mindfire Solutions
Upcoming SlideShare
Loading in...5
×

It seminar-xml serialization

46

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
46
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

It seminar-xml serialization

  1. 1. Introduction to Xml Serialization in C# Presenter: Priyojit Mondal, Mindfire Solutions Date: 20/12/2013
  2. 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. 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. 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. 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. 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. 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
  8. 8. Demo Presenter: Priyojit Mondal, Mindfire Solutions
  9. 9. Customization      XmlRoot XmlElement XmlAttribute XmlText XmlEnum Presenter: Priyojit Mondal, Mindfire Solutions
  10. 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. 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
  12. 12. Troubleshooting Case 1: Declaring Serialization Types. Case 2: Working with a class with parametrized constructor. Presenter: Priyojit Mondal, Mindfire Solutions
  13. 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
  14. 14. Question and Answer Presenter: Priyojit Mondal, Mindfire Solutions
  15. 15. Thank you Presenter: Priyojit Mondal, Mindfire Solutions
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×