Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • C# .Net : XML Serialization of DataSet

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 685
    Comment on it

    C# .Net : XML Serialization of DataSet

     

    XML Serialization require XmlSerializer class which is derived from System.Xml.Serialization.

     

    Example to demonstrate XML Serialization of DataSet :-

     

    using System.IO;
    
    namespace DemoApplication
    {
        class DatasetSerialization
        {
            public static void Main(String[] args)
            {
                // XmlSerializer class instance of type DataSet is created
                XmlSerializer serializer = new XmlSerializer(typeof(DataSet));
    
                //creation of DataSet
                DataSet employeeDS = new DataSet("Employee");
    
                //creation of DataTable
                DataTable empdetailsDT = new DataTable("EmployeeDetails");
    
                //Columns of DataTable
                empdetailsDT.Columns.Add("EmployeeName");
                empdetailsDT.Columns.Add("EmployeeDepartment");
    
                //Rowsc of Datatable
                empdetailsDT.Rows.Add("Deepak Rana", "Mechanical");
                empdetailsDT.Rows.Add("Suraj Rana", "HM");
    
                //Adding DataTable to DataSet
                employeeDS.Tables.Add(empdetailsDT);
    
                //StreamWriter object assigned to TextWriter instance
                TextWriter textWriter = new StreamWriter(@"D:\BasicDataSetSerialization.xml");
    
                //Serialize the Datset using serialize method
                serializer.Serialize(textWriter, employeeDS);
    
                textWriter.Close();
            }
        }
    }

     

    Steps used in above program :-

    • An object of XmlSerializer class is created of type "DataSet",now this object will be used to serialize.

     

    • DataSet is defined.

     

    • DataTable with rows and columns is defined which is then added to Dataset. 

     

    • An instance of TextWriter is created which is assigned to StreamWriter object for saving file in some physical location. TextWriter is an abstract class.

     

    • “Serialize” method of xmlSerializer class is called which performs xml serialization of defined dataset object of type.

     

    • Finally TextWriter is closed.

     

    When above program is run an xml file named "BasicDataSetSerialization.xml" is created in mentioned physical location. The contents of this file is as follows :-

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: