Read XML File into DataSet

by CameronM 9. March 2010 17:55

There are a number of ways to read an XML file and use the contents in .NET. One nice way, for people who are familiar with using DataSets is the ReadXML method of the DataSet Class. This method reads an XML file into a DataSet that can then be used to retreive and manipulate the elements via standard DataTable and DataRow methods.

In this example we have created a simple XML file used to store application settings that need to be shared amongst several applications. It is bascially a trimmed-down version of the app.config file Visual Studio creates whenever you add settings to s project via the IDE. 

<?xml version="1.0" encoding="utf-8" ?>
<AppSettings>
    <setting name="CurrentInstance" serializeAs="String">
      <value>1</value>
    </setting>
    <setting name="OutputDirectory" serializeAs="String">
      <value>\\SV-OCRMGR\mbrc\ocr\xmloutput\test2</value>
    </setting>
</AppSettings>

To retrieve the value of any setting, we need to grab the setting node from the XML file and return the child element called value.

When we load the DataSet, in this simple case it consists of just one DataTable, called settings. The settings tables consists of two rows. One interesing thing to note is that when the child element returns a single value, there is not distinction made between child elements and attributes. The child element value and the attribute name will both be treated as columns in the resulting DataTable.

//declare a new DataSet
DataSet ds = new DataSet();
 
//read the XML file into the DataSet
ds.ReadXml("AppSettings.xml");
 
//access each node in the XML file using a DataRow
//in this example the nodes we want are in the settings table
foreach (DataRow drResult in ds.Tables["setting"].Rows)
{
     //access the elements of the XML file using the DataRow columns
     //we will just write them to Trace for now
     System.Diagnostics.Trace.Write(drResult["name"].ToString() + " ");
     System.Diagnostics.Trace.WriteLine(drResult["value"].ToString());
}

The output for this procedure is shown below;

CurrentInstance 1
OutputDirectory \\SV-OCRMGR\mbrc\ocr\xmloutput\test2

To see how .NET DataSets handle more complex XML, we can modify the original XML file so that in addition to the value child element, setting also had a complex child element called currentStatus. This element has a number of sub-elements, namely status and lastUpdated.

<?xml version="1.0" encoding="utf-8" ?>
<AppSettings>
    <setting name="CurrentInstance" serializeAs="String">
      <value>1</value>
      <currentStatus>
        <status>live</status>
        <lastUpdated></lastUpdated>
      </currentStatus>
    </setting>
    <setting name="OutputDirectory" serializeAs="String">
      <value>\\SV-OCRMGR\mbrc\ocr\xmloutput\test2</value>
      <currentStatus>
        <status>test</status>
        <lastUpdated></lastUpdated>
      </currentStatus>
    </setting>
</AppSettings>

If we run our code, nothing seems to have changed, however if we add a breakpoint we will see the the DataSet contains two DataTables, setting and currentStatus. In addition, .NET has added a new column to both tables called setting_Id. This is the primary-foreign key between the two tables that is used to link the values in setting to those in currentStatus. Read the next post as we investigate how .NET handles more complicated XML files.

Tags: , , ,

VB.NET | VB.NET | XML | XML