SlideShare una empresa de Scribd logo
1 de 15
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

Más contenido relacionado

La actualidad más candente

La actualidad más candente (17)

Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
 
core java
core javacore java
core java
 
Java Simple Notes
Java Simple NotesJava Simple Notes
Java Simple Notes
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
Bab satu
Bab satuBab satu
Bab satu
 
Session 10 - OOP with Java - Abstract Classes and Interfaces
Session 10 - OOP with Java - Abstract Classes and InterfacesSession 10 - OOP with Java - Abstract Classes and Interfaces
Session 10 - OOP with Java - Abstract Classes and Interfaces
 
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Train...
 
C plusplus
C plusplusC plusplus
C plusplus
 
Core java
Core javaCore java
Core java
 
Inner Classes
Inner Classes Inner Classes
Inner Classes
 
PATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and JavaPATTERNS09 - Generics in .NET and Java
PATTERNS09 - Generics in .NET and Java
 
Reflection and Introspection
Reflection and IntrospectionReflection and Introspection
Reflection and Introspection
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Java OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - InheritanceJava OOP Programming language (Part 5) - Inheritance
Java OOP Programming language (Part 5) - Inheritance
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
Reflection
ReflectionReflection
Reflection
 

Similar a It seminar-xml serialization (20)

The xml
The xmlThe xml
The xml
 
Working with xml data
Working with xml dataWorking with xml data
Working with xml data
 
Xml serialization
Xml serializationXml serialization
Xml serialization
 
06 xml processing-in-.net
06 xml processing-in-.net06 xml processing-in-.net
06 xml processing-in-.net
 
LINQ to XML
LINQ to XMLLINQ to XML
LINQ to XML
 
Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12Advanced Web Programming Chapter 12
Advanced Web Programming Chapter 12
 
Working with XML and JSON Serializing
Working with XML and JSON SerializingWorking with XML and JSON Serializing
Working with XML and JSON Serializing
 
Session 5
Session 5Session 5
Session 5
 
DOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om SivanesianDOSUG XML Beans overview by Om Sivanesian
DOSUG XML Beans overview by Om Sivanesian
 
Xml representation oftextspecifications
Xml representation oftextspecificationsXml representation oftextspecifications
Xml representation oftextspecifications
 
Ado.net session11
Ado.net session11Ado.net session11
Ado.net session11
 
Applied xml programming for microsoft
Applied xml programming for microsoftApplied xml programming for microsoft
Applied xml programming for microsoft
 
XML
XMLXML
XML
 
Ch23
Ch23Ch23
Ch23
 
Ch23 xml processing_with_java
Ch23 xml processing_with_javaCh23 xml processing_with_java
Ch23 xml processing_with_java
 
Applied xml programming for microsoft 3
Applied xml programming for microsoft 3Applied xml programming for microsoft 3
Applied xml programming for microsoft 3
 
04 sm3 xml_xp_07
04 sm3 xml_xp_0704 sm3 xml_xp_07
04 sm3 xml_xp_07
 
04 sm3 xml_xp_08
04 sm3 xml_xp_0804 sm3 xml_xp_08
04 sm3 xml_xp_08
 
Dom
Dom Dom
Dom
 
Xml and xml processor
Xml and xml processorXml and xml processor
Xml and xml processor
 

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

It seminar-xml serialization

  • 1. Introduction to Xml Serialization in C# Presenter: Priyojit Mondal, Mindfire Solutions Date: 20/12/2013
  • 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
  • 8. Demo 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
  • 12. Troubleshooting Case 1: Declaring Serialization Types. Case 2: Working with a class with parametrized constructor. 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
  • 14. Question and Answer Presenter: Priyojit Mondal, Mindfire Solutions
  • 15. Thank you Presenter: Priyojit Mondal, Mindfire Solutions