How to serialize a .Net Object to XML

Serialization of XML to common language runtime (CLR) objects allows for the conversion of XML documents into a format that can be easily processed using traditional programming languages. In the .NET Framework, XML is a widely supported file format, and the framework provides classes that enable reading, writing, and other operations on XML-formatted files.

XML serialization

In XML serialization, the primary objective is to transform a Dataset object into an XML representation that can be stored as a file on disk. To achieve this, the program utilizes the XmlSerializer class, which is specifically designed for XML serialization.

By employing the XmlSerializer class, the Dataset object is serialized, or converted, into an XML format. This serialized data can then be written to a disk file, preserving the structure and contents of the original Dataset object.

Serialization offers several benefits, such as the ability to transport data in a standardized, platform-independent manner. Additionally, serialized XML data can be easily shared, stored, and processed by various applications and systems.

Dim serialWriter As StreamWriter serialWriter = New StreamWriter("serialXML.xml") Dim xmlWriter As New XmlSerializer(ds.GetType()) xmlWriter.Serialize(serialWriter, ds) serialWriter.Close()

The XmlSerializer class in .NET provides a convenient and straightforward approach to XML serialization. It enables developers to define the structure and behavior of the serialization process through attributes and configuration options. This flexibility allows for customization and control over how the Dataset object is serialized and deserialized.

The following program shows how to serialize a Dataset to an XML disk file . Here we are using XmlSerializer class for serialize the Dataset Object.

Full Source VB.NET
Public Class Form1 Dim dt As DataTable Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim ds As New DataSet dt = New DataTable() dt.Columns.Add(New DataColumn("Product_ID", Type.GetType("System.Int32"))) dt.Columns.Add(New DataColumn("Product_Name", Type.GetType("System.String"))) dt.Columns.Add(New DataColumn("product_Price", Type.GetType("System.Int32"))) fillRows(1, "product1", 9999) fillRows(2, "product2", 2222) fillRows(3, "product3", 3333) fillRows(4, "product4", 4444) ds.Tables.Add(dt) ds.Tables(0).TableName = "product" Dim serialWriter As StreamWriter serialWriter = New StreamWriter("serialXML.xml") Dim xmlWriter As New XmlSerializer(ds.GetType()) xmlWriter.Serialize(serialWriter, ds) serialWriter.Close() ds.Clear() End Sub Private Sub fillRows(ByVal pID As Integer, ByVal pName As String, ByVal pPrice As Integer) Dim dr As DataRow dr = dt.NewRow() dr("Product_ID") = pID dr("Product_Name") = pName dr("product_Price") = pPrice dt.Rows.Add(dr) End Sub End Class

Click here to download serialXML.xml

Conclusion

XML serialization in the .NET Framework is a powerful mechanism for converting XML documents into a format compatible with CLR objects. The XmlSerializer class facilitates this process by providing a robust set of features and options for customization. By using XML serialization, developers can effectively work with XML data in their applications, enabling seamless interoperability and data exchange.