XML Serialization
Serialization of XML to Common Language Runtime Objects enables one to convert XML documents into a form where they are easier to process using conventional programming languages. The .Net technology is widely supported XML file format. The .Net Framework provides the Classes for read, write, and other operations in XML formatted files .
The following C# 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 C#
using System;
using System.Data;
using System.Windows.Forms;
using System.Xml.Serialization ;
using System.IO;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = 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";
StreamWriter serialWriter ;
serialWriter = new StreamWriter("serialXML.xml");
XmlSerializer xmlWriter = new XmlSerializer(ds.GetType());
xmlWriter.Serialize(serialWriter, ds);
serialWriter.Close();
ds.Clear();
}
private void fillRows(int pID, string pName, int pPrice)
{
DataRow dr ;
dr = dt.NewRow();
dr["Product_ID"] = pID;
dr["Product_Name"] = pName;
dr["product_Price"] = pPrice;
dt.Rows.Add(dr);
}
}
}
Click here to download serialXML.xml
Related Topics
- How to XML in C#
- How to create an XML file in C#
- How to open and read XML file in C# , XmlReader , XmlTextReader , XmlDocument
- How to create XML file from Dataset
- How to Open and read an XML file to Dataset
- How to create an XML file from SQL
- How to search in a XML file
- How to filter in a XML file
- How to insert data from XML to database
- How to create Excel file from XML
- How to create XML file from Excel
- How to XML to DataGridView
- How to create a TreeView from XML File
- How to create a Crystal Reports from XML File
- XML Serialization Tutorial
- XML DeSerialization