A simple how to export from TableAdapter to XML file

The XML Schema CompanionI recently wanted to import data from an existing system that is written in Access. The system allows for XML imports. Since I needed to do some background programming on the system, I opted to rather to it programmatically than use the standard export, since I would have to do it a few times, and also manipulate the data once it was imported and then exported.
I struggled to find information on how to do this, so eventually decided to figure it out myself and this is how I did it:
  1. In Visual Studio I created a new project
  2. I added Connected to Access database using the Database Explorer, this created a link in the App_Data folder
  3. Then on the default page I allowed the user to place the file name,
  4. I then added a new DataSet, created a new TableAdapter, put the query I needed in there and
  5. Then in my code I exported to XML

So the in Default.aspx It looks like this:

File to Export to:
<asp:TextBox ID="tbxCustomersToXML" runat="server" Width="375px" ToolTip="XML File Target" 
\Documents\junk\Customers.XML" />
<asp:Button ID="btnCustomersToXML" runat="server" Text="Customers TBL to XML" onclick
="btnCustomersToXML_Click" />
<br />
<asp:Label ID ="lblStatus" runat="server" />
<br />

And the in Default.aspx.cs looks like this:

protected void btnCustomersToXML_Click(object sender, 
EventArgs e)
  if (tbxCustomersToXML.Text != "")
         CustomersTblTableAdapter dtaCompanys = new CustomersTblTableAdapter();
         // retrieve the data using GetData since this is where the correct Query is
         TrackerCompaniesDataSet.CustomersTblDataTable dtCompanys = dtaCompanys.GetData();
         System.IO.StreamWriter swXMLTarget = File.CreateText(tbxCustomersToXML.Text);

         dtCompanys.WriteXml(swXMLTarget, XmlWriteMode.WriteSchema);
         lblStatus.Text = "Extract status: Done!";
       catch (Exception ex)
         lblStatus.Text = "Extract status: The file could not be exported. The following error occurred: " + ex.Message;

Hope that this helps someone, I may need to refer to it myself.


Popular Posts