ADO - XML Serialization of DataSet and DataTable in ADO.NET
XML Serialization is the process of converting objects such as DataSet and DataTable into XML format so that the data can be stored, transferred, or shared between different systems. In ADO.NET, XML serialization is an important feature because it allows database data to be represented in a platform-independent and human-readable format.
A DataSet is designed with built-in XML support. It can easily read and write XML data without requiring additional libraries. This makes it suitable for web services, configuration storage, data exchange, and distributed applications.
Why XML Serialization is Important
XML serialization provides several advantages in application development:
-
Data Portability
XML data can be transferred between applications running on different platforms and programming languages. -
Offline Data Storage
Data can be saved into XML files and loaded later without connecting to the database. -
Web Service Communication
XML is widely used in SOAP-based web services and enterprise systems. -
Human Readability
XML files can be opened and understood using any text editor. -
Structured Data Representation
XML maintains relationships between tables, rows, and columns.
Serialization of DataSet
A DataSet can contain multiple tables, relationships, constraints, and data. ADO.NET provides methods to serialize the complete structure and content into XML.
Methods Used for XML Serialization
1. WriteXml()
This method writes the contents of a DataSet into an XML file.
Syntax
DataSet.WriteXml(string filename);
Example
using System;
using System.Data;
class Program
{
static void Main()
{
DataSet ds = new DataSet("StudentData");
DataTable dt = new DataTable("Students");
dt.Columns.Add("ID");
dt.Columns.Add("Name");
dt.Columns.Add("Course");
dt.Rows.Add("101", "Rahul", "C#");
dt.Rows.Add("102", "Sneha", "ADO.NET");
ds.Tables.Add(dt);
ds.WriteXml("students.xml");
Console.WriteLine("XML file created successfully.");
}
}
Output XML
<StudentData>
<Students>
<ID>101</ID>
<Name>Rahul</Name>
<Course>C#</Course>
</Students>
<Students>
<ID>102</ID>
<Name>Sneha</Name>
<Course>ADO.NET</Course>
</Students>
</StudentData>
The XML file stores the data in a hierarchical structure.
Serialization with Schema
Sometimes only storing data is not enough. The application may also require table structure information such as:
-
Column names
-
Data types
-
Constraints
-
Relationships
In such cases, XML Schema is used.
WriteXmlSchema()
This method writes only the schema information.
Example
ds.WriteXmlSchema("studentsSchema.xml");
Writing Both Data and Schema Together
ADO.NET allows combining data and schema in a single XML file.
XmlWriteMode Enumeration
The WriteXml() method supports different modes.
Syntax
ds.WriteXml("students.xml", XmlWriteMode.WriteSchema);
Example
using System;
using System.Data;
class Program
{
static void Main()
{
DataSet ds = new DataSet("Company");
DataTable dt = new DataTable("Employees");
dt.Columns.Add("EmpID", typeof(int));
dt.Columns.Add("EmpName", typeof(string));
dt.Columns.Add("Salary", typeof(double));
dt.Rows.Add(1, "Amit", 45000);
dt.Rows.Add(2, "Neha", 52000);
ds.Tables.Add(dt);
ds.WriteXml("employees.xml", XmlWriteMode.WriteSchema);
Console.WriteLine("XML with schema created.");
}
}
The generated XML contains both the table structure and the data.
Reading XML into DataSet
ADO.NET can also deserialize XML data back into a DataSet.
ReadXml()
This method reads XML data and reconstructs the tables.
Example
using System;
using System.Data;
class Program
{
static void Main()
{
DataSet ds = new DataSet();
ds.ReadXml("students.xml");
foreach (DataRow row in ds.Tables[0].Rows)
{
Console.WriteLine(row["ID"] + " " + row["Name"]);
}
}
}
Output
101 Rahul
102 Sneha
The XML data is converted back into rows and columns.
Serialization of DataTable
A DataTable can also be serialized independently without using a DataSet.
Example
using System;
using System.Data;
class Program
{
static void Main()
{
DataTable dt = new DataTable("Products");
dt.Columns.Add("ProductID");
dt.Columns.Add("ProductName");
dt.Rows.Add("P101", "Keyboard");
dt.Rows.Add("P102", "Mouse");
dt.WriteXml("products.xml");
Console.WriteLine("DataTable serialized.");
}
}
Reading XML into DataTable
DataTable dt = new DataTable();
dt.ReadXml("products.xml");
This recreates the table data from the XML file.
XmlWriteMode Options
ADO.NET provides different modes for XML writing.
1. IgnoreSchema
Writes only data.
ds.WriteXml("file.xml", XmlWriteMode.IgnoreSchema);
2. WriteSchema
Writes both data and schema.
ds.WriteXml("file.xml", XmlWriteMode.WriteSchema);
3. DiffGram
Stores original and modified versions of data.
ds.WriteXml("file.xml", XmlWriteMode.DiffGram);
Understanding DiffGram
DiffGram is useful for tracking changes in data.
It stores:
-
Original values
-
Current values
-
Row states
-
Deleted rows
This is useful in distributed systems where synchronization is required.
Example
ds.WriteXml("changes.xml", XmlWriteMode.DiffGram);
XML Serialization with Relationships
A DataSet can store parent-child relationships between tables.
Example
DataRelation relation = new DataRelation(
"DeptEmp",
deptTable.Columns["DeptID"],
empTable.Columns["DeptID"]
);
ds.Relations.Add(relation);
When serialized, XML preserves these relationships in hierarchical form.
Advantages of XML Serialization
1. Easy Data Exchange
Applications can share XML regardless of operating system or language.
2. Database Independence
Data can be stored and transferred without maintaining a database connection.
3. Supports Hierarchical Data
XML naturally represents nested relationships.
4. Built-in Support
ADO.NET provides direct methods without requiring custom serialization code.
5. Useful for Web Applications
Many APIs and services use XML communication.
Limitations of XML Serialization
1. Large File Size
XML files are verbose and consume more storage.
2. Slower Processing
Parsing XML is slower compared to binary formats.
3. Complex Structure
Large datasets can produce complicated XML documents.
4. Memory Consumption
Serialization of very large datasets can increase memory usage.
Real-World Applications
1. Data Backup
Applications save temporary data into XML files.
2. Web Services
SOAP services exchange XML messages.
3. Configuration Storage
Application settings are stored in XML format.
4. Offline Applications
Desktop applications can work without continuous database connectivity.
5. Data Synchronization
DiffGram helps synchronize modified data between systems.
Difference Between Serialization and Deserialization
| Serialization | Deserialization |
|---|---|
| Converts object into XML | Converts XML back into object |
| Uses WriteXml() | Uses ReadXml() |
| Saves data | Loads data |
| Used for transfer/storage | Used for reconstruction |
Best Practices
-
Use
WriteSchemawhen structure information is important. -
Validate XML before reading external files.
-
Use DiffGram only when change tracking is required.
-
Avoid serializing extremely large datasets into XML.
-
Use proper exception handling while reading XML files.
Conclusion
XML Serialization in ADO.NET allows DataSet and DataTable objects to be converted into XML format for storage, transfer, and communication. Using methods such as WriteXml(), ReadXml(), and WriteXmlSchema(), developers can easily serialize and deserialize relational data. This feature plays a major role in disconnected applications, web services, data exchange systems, and distributed enterprise environments.