XML - Deserialization
What Deserialization Is
-
Serialization = convert an object → XML (or another format).
-
Deserialization = the reverse: convert XML → object in memory.
-
It allows programs to reconstruct data structures from XML text.
Why It’s Useful
-
Data can be stored or transmitted as XML, then recreated as objects in another program or on another system.
-
Common in:
-
Web services / APIs (XML responses mapped to objects).
-
Config files (load settings into objects).
-
Data exchange between different platforms.
-
Example (C# style)
XML file:
<Book>
<Title>XML Guide</Title>
<Author>A. Smith</Author>
</Book>
Class:
public class Book {
public string Title { get; set; }
public string Author { get; set; }
}
Deserialization code:
XmlSerializer serializer = new XmlSerializer(typeof(Book));
using (FileStream fs = new FileStream("book.xml", FileMode.Open)) {
Book b = (Book)serializer.Deserialize(fs);
}
Now b.Title
= "XML Guide"
, b.Author
= "A. Smith"
.
Deserialization vs Parsing
-
Parsing: just reading XML structure (like SAX, DOM, StAX).
-
Deserialization: goes further — it maps XML elements directly to object fields/properties.
Advantages
-
Easy way to reconstruct complex objects.
-
Automates data binding between XML and code.
Disadvantages
-
Requires schema/class definition.
-
Sensitive to mismatches between XML structure and object structure.
In short:
Deserialization = taking XML text and rebuilding it into program objects, making XML data usable directly in code.