Deserializing a Data Set from XML

Deserializing a Data Set from XML

You can easily create a DataSet by deserializing an XML file or stream. When a schema is not provided, all XML data is treated as string data, so you should first load the schema if it is in a separate file.

The following code sample shows how to read the schema file and then load the XML file in a DataSet.

 

using System.Data;

using System.Data.SqlClient;

namespace XMLToDataSet

{

    class Program

    {

        static void Main(string[] args)

        {

            string strFileName = @"..\..\Customer.xml";

            DataSet objDataSet = new DataSet();

            objDataSet.ReadXmlSchema(strFileName);

            objDataSet.ReadXml(strFileName, XmlReadMode.IgnoreSchema);           

        }     

    }

}

 

In this example we have used  XmlReadMode.IgnoreSchema , so if the XML data file contains an XML schema definition, it is ignored. XmlReadMode enumeration Specifies how to read XML data and a relational schema into a DataSet.

XmlReadMode Enumeration Members includes Auto, DiffGram, Fragment, IgnoreSchema, InferSchema, InferTypedSchema, ReadSchema.



Ebook Download
View all
Learn
View all