Using DataSets to Process XML

I started a project recently that requires me to process an XML file from Google. Being the lazy sort, I’d really rather just use the data as though it were part of a database and forget that it was ever XML. So I’m using a method in the DataSet class that I’m finding is rather obscure. ReadXml().

This little function will take ANY XML file path as a parameter and will load it into an untyped dataset. I’ve used it before to load RSS feeds and databind to them. You’ll need to run the debugger to find out exactly which table in the dataset has the data you are interested in. In this case, I found that Tables[4] was the table I wanted.

DataSet ds = new DataSet();
ds.ReadXml(openFileDialog1.FileName);
foreach(DataRow dr in ds.Tables[4].Rows)

But as I pondered the situation more, I realized that what I really wanted was the XML in a typed dataset. Creating a typed dataset with the name of the table and the rows in that table with the same name as what they showed up in my untyped dataset didn’t quite do the trick. However, I was able to use this loop to get the data out of my untyped dataset into my typed dataset without having to spend a lot of time determining which fields were in both. In fact, the code below will allow me to change my typed dataset and/or the data I’m having Google put in my XML file without having to change this loop at all.

DataSetAdWords dsa = new DataSetAdWords();
DataSet ds = new DataSet();
ds.ReadXml(openFileDialog1.FileName);
foreach(DataRow dr in ds.Tables[4].Rows)
{
    DataSetAdWords.rowRow r = dsa.row.NewrowRow();
    foreach(DataColumn dc in dsa.row.Columns)
    {
        r.SetField(dc,
            dr.ItemArray[ds.Tables[4].Columns[dc.ColumnName].Ordinal]);

    }
    dsa.row.AddrowRow(r);
}

Like this Article? Subscribe to get every article sent to your email.

Related Post

  • Swapping Out the DALSwapping Out the DAL One of the comments that showed up on the blog post about using 3-tiered with DataSets and the ObjectDataSource was, "How do we set this up so that we can swap out the DAL?"  The […]
  • Silverlight – Databinding to a Web ServiceSilverlight – Databinding to a Web Service In the last several posts, I’ve demonstrated various ways of getting data onto the screen.  What I’ve purposely omitted up until this point is how to update this information. So […]
  • One Form with Multiple TablesOne Form with Multiple Tables I received the following question a few days ago: “I am trying to use three tables from the same dataset in one form that I am creating in vs.net winforms my problem I guess is the setup […]
  • DotNetNuke Modules – Creating Base ModulesDotNetNuke Modules – Creating Base Modules Now that we have DotNetNuke installed into Visual Studio we can go ahead and create our first modules. Actually, creating the modules is pretty simple. But it is even easier to do it […]
  • DataSets, StoredProcedures and 3-Tier ASP.NETDataSets, StoredProcedures and 3-Tier ASP.NET Last Friday I showed how to use DataSet, the ObjectDataSource and select statements.  Today, I'll start with the project we ended up with last week and convert it to use stored […]

About Dave Bush

Dave Bush is a Full Stack ASP.NET developer. His commitment to quality through test driven development, vast knowledge of C#, HTML, CSS and JavaScript as well as his ability to mentor younger programmers and his passion for Agile/Scrum as defined by the Agile Manifesto and the Scrum Alliance will certainly be an asset to your organization.

  • http://wildermuth.com Shawn Wildermuth

    Be aware that this does not work with any ad-hoc XML as the DataSet can’t consume XML where one typed child can have more than one typed parent. Have you considered LINQ to XSD as an alternative?

  • Dave

    I haven’t run into that situation and this isn’t the first time I’ve tried this. So, until I do, I’m going to continue to use this simple method.