Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
# Thursday, March 27, 2008

Last week I showed you ways on how to read text files. This time around we will be looking at how to load XML files into a DataSet.

Let's assume that we have a file called Symbols.xml which contains some XML data.

<?xml version="1.0" encoding="utf-8" ?>
<Symbols>
    <Symbol ExecutionSymbol="ATT" Name="AT&amp;T"></Symbol>
    <Symbol ExecutionSymbol="MSFT" Name="Microsoft"></Symbol>
    <Symbol ExecutionSymbol="GOOG" Name="Google"></Symbol>
    <Symbol ExecutionSymbol="CSCO" Name="Cisco"></Symbol>
    <Symbol ExecutionSymbol="IP" Name="International Paper Co."></Symbol>
    <Symbol ExecutionSymbol="MF" Name="MF Global"></Symbol>
    <Symbol ExecutionSymbol="Q" Name="Qwest Communications International Inc."></Symbol>
    <Symbol ExecutionSymbol="BMC" Name="BMC Software Inc."></Symbol>
    <Symbol ExecutionSymbol="WCI" Name="WCI Communities Inc."></Symbol>
    <Symbol ExecutionSymbol="SPY" Name="SDRs"></Symbol>
    <Symbol ExecutionSymbol="LEH" Name="Lehman Brothers Holdings Inc."></Symbol>
    <Symbol ExecutionSymbol="XLF" Name="Financial Select Sector SPDR"></Symbol>
    <Symbol ExecutionSymbol="QQQQ" Name="PowerShares QQQ TR 1"></Symbol>
    <Symbol ExecutionSymbol="IWM" Name="IShare Rus 2000 INDX"></Symbol>
    <Symbol ExecutionSymbol="GE" Name="General Electric Co."></Symbol>
    <Symbol ExecutionSymbol="MER" Name="Merrill Lynch Co., Inc."></Symbol>
    <Symbol ExecutionSymbol="BAC" Name="Bank of America Corporation"></Symbol>
    <Symbol ExecutionSymbol="INTC" Name="Intel Corp"></Symbol>
    <Symbol ExecutionSymbol="F" Name="Ford Motor Co."></Symbol>
    <Symbol ExecutionSymbol="QID" Name="UltraShort QQQ ProShares"></Symbol>
</Symbols>

And you want to load that data into a DataSet and display it to a GridView. How would you do it?

There are different ways you can load an XML file to a DataSet and i'm here to show you 4 of them. I don't have alot of time to explain the intrinsicts of each sample or performance comparisson of each technique because of time constraints so i'll leave that part for you to figure out the best among this examples. ;)

SAMPLE 1

//Sample 1
//the code below shows us how to load the XML document
//directly to our dataset by just specifying the file
//location to the DataSet.ReadXml method of the DataSet

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //create the Dataset object
            using (DataSet ds = new DataSet())
            {
                //load the xml data to the dataset
                ds.ReadXml(xmlFilePath);

                //bind the values to our GridView
                symbolGridView.Caption = "<h4>Loaded the data using Sample1</h4>";
                symbolGridView.DataSource = ds;
                symbolGridView.DataBind();
            }
        }
    }
}

SAMPLE 2

//Sample 2
//The code below is using the TextReader object to read
//the contents of the file and then assign those values
//to our dataset object

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the TextReader object
            using (TextReader sReader = new StreamReader(xmlFilePath))
            {
                //create the Dataset object
                using (DataSet ds = new DataSet())
                {
                    //load the xml data from the TextReader to the Dataset
                    ds.ReadXml(sReader);

                    //bind the values to our GridView
                    symbolGridView.Caption = "<h4>Loaded the data using Sample2</h4>";
                    symbolGridView.DataSource = ds;
                    symbolGridView.DataBind();
                }
            }
        }
    }
}

SAMPLE 3

//Sample 3
//The code below is basically the same as the above code
//but this time we are using the XmlTextReader object
//instead of using the TextReader object. Please note that
//the XmlReader object calls the TextReader internally.

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the XmlTextReader object
            using (XmlTextReader xTextReader = new XmlTextReader(xmlFilePath))
            {
                //create the Dataset object
                using (DataSet ds = new DataSet())
                {
                    //load the xml data to dataset
                    ds.ReadXml(xTextReader);

                    //bind the values to our GridView
                    symbolGridView.Caption = "<h4>Loaded the data using Sample3</h4>";
                    symbolGridView.DataSource = ds;
                    symbolGridView.DataBind();
                }
            }
        }
    }
}

SAMPLE 4

//Sample 4
//The code below uses an XmlDocument object and reads a file
//using the XmlDocument.LoadXml method. That data is then
//transfered to a DataSet by using the DataSet.ReadXml method
//and passing a XmlNodeReader object based on our XmlDocument

using System;
using System.Data;
using System.Xml;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string xmlFilePath = Server.MapPath(@"App_Data/Symbols.xml");

        if (File.Exists(xmlFilePath))
        {
            //Create the Dataset object
            using (DataSet ds = new DataSet())
            {
                //create the XmlDocument object
                XmlDocument xDoc = new XmlDocument();

                //load the contents of the file to our XmlDocument object
                xDoc.LoadXml(File.ReadAllText(xmlFilePath));

                //load the xml data in the XmlDocument object to the Dataset
                ds.ReadXml(new XmlNodeReader(xDoc));

                //bind the values to our GridView
                symbolGridView4.Caption = "<h4>Loaded the data using Sample4</h4>";
                symbolGridView4.DataSource = ds;
                symbolGridView4.DataBind();
            }
        }
    }
}

And that's it. On my next post i'll show you how to load XML data directly to the GridView using the XMLDataSource object.

HTH.

Thursday, March 27, 2008 3:47:22 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
.NET | Tutorial
Archive
<March 2008>
SunMonTueWedThuFriSat
2425262728291
2345678
9101112131415
16171819202122
23242526272829
303112345
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2010
Keith Rull
Sign In
Statistics
Total Posts: 271
This Year: 0
This Month: 0
This Week: 0
Comments: 182
Themes
Pick a theme:
All Content © 2010, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)