Thursday 1 April 2010

Read data from XML and store into Data Set Using C#

I am using XML file as config file in my automation tool which I have developed to store all the inputs/settings/run time config value etc etc. Because it easy for manual guys also to just open XML file in Notepad and alter the setting based on their requirement and it’s easy to maintain also.

Below is the code which I am using to Read data from XML file and store in to Data set for further use.

//Local vaible to get xml path
string xmlPath=”Inputs.xml”;
//Read all the value to the xml read command
System.IO.FileStream fsReadXml = new System.IO.FileStream(xmlPath,
System.IO.FileMode.Open);
//Declare a dataset to hold all the XML value
DataSet ds = new DataSet();
try
{
//Read the value from XML reader to the dataset
ds.ReadXml(fsReadXml);

}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
//Close the reader.
fsReadXml.Close();
}
//your dataset (ds) would be having all the data stored from XML file.

Thanks,
Md.Jawed