Web Design that taps into the haromny and vision of your dreams.

Importing and Exporting XML from a DataSet

Using XML Datasets

Written By on in C#

266 words, estimated reading time 2 minutes.

The .Net Framework and ADO.Net both contain strong XML support; in fact, XML forms the backbone of data communication in .Net. One of the features you will almost certainly need to use when dealing with 3rd party data providers is to import or export XML data to and from SQL Server and/or managed code.

Active Data Objects (ADO.Net) Series
  1. What are Active Data Objects?
  2. Connecting to a SQL Server Database with C#
  3. Consuming Data in Windows Forms C# Applications
  4. Using ADO.Net Controls in ASP.Net Pages
  5. Modifying Data and Updating Databases with C#
  6. Connecting to MySql Databases with C# / ASP.Net
  7. Reading Excel Spreadsheets with C# ADO.Net
  8. Creating SQL Server Stored Procedures in C#
  9. Calling SQL Server Stored Procedures from C#
  10. Importing and Exporting XML from a DataSet

.Net provides a DataSet object which is used to query SQL Server databases, but it also has the functionality to read and write XML data, either in the form of a string, text reader object, and XML reader object or an IO stream. In each instance, you will need to call the ReadXml method of the DataSet object or the WriteXml method.

Reading XML Data using a DataSet

For this example I am going to use the sample CD catalogue data from W3Schools, and display it using an ASP.Net grid view.

Once you have added a Grid View to an ASP document:

<asp:GridView ID="GridView1" runat="server"></asp:GridView>

You only need a small amount of C# code to import the CD catalogue and output the data.

DataSet ds = new DataSet();
ds.ReadXml("c:\cd_catalog.xml");
 
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();

Writing SQL Records to XML with a DataSet

The DataSet object contains a method for writing XML data to disk and can be used to serialise SQL Server data. This example uses the Products table of the Northwind sample database.

string connectionString="Data Source=.SQLEXPRESS;AttachDbFilename="C:\SQL Server 2000\Sample Databases\NORTHWND.MDF";Integrated Security=True;Connect Timeout=30;User Instance=True";
string commandString = "SELECT * FROM Products";
 
SqlDataAdapter da = new SqlDataAdapter(commandString, connectionString);
DataSet ds = new DataSet();
da.Fill(ds);
ds.WriteXml("c:products.xml");

Of course, in both examples, you can customise the data by sorting, filtering and so on.

Last updated on: Thursday 22nd June 2017

 

Comments

There are no comments for this post. Be the first!

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me