.NET Answers

ASP.NET, HTML, CSS, Visual Studio, CSharp, VB.NET and other programming items of interest.
Subscribe
  • Home
  • About Me
  • Advertising
  • Click Here to Ask a question
    • Privacy Policy
  • Site Map

Using DataSets to Process XML

August 06, 2008 By: Dave

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);
}

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

Related Post

  • Swapping Out the DAL
  • Twitter from ASP.NET – Simple GUI
  • Twitter from ASP.NET
Bookmark to:

Add to Del.icio.us Add to digg Add to DotNetKicks Add to DZone Add to Facebook Add to Slashdot Add to Stumble Upon Add to Technorati
Hide Sites
Tags: asp.net, csharp, datasets, xml

2 Responses to “ Using DataSets to Process XML ”

  1. # 1 Shawn Wildermuth Says:
    August 6th, 2008 at 4:52 am

    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?

  2. # 2 Dave Says:
    August 6th, 2008 at 5:38 am

    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.

← DotNetNuke Modules – Data Access Layer
DotNetNuke Modules – Data Access Layer →
  • Search

  • Subscribe

    U COMMENT
    I FOLLOW

    Subscribe in a reader

    OR

    Subscribe via e-mail

    Enter your email address: 

    Delivered by FeedBurner

     

  • Follow Me

    • Twitter
    • FaceBook
    • Digg
    • StumbleUpon
    • Propeller
    • Delicious
    • Plaxo

     

  • Recent Posts

    • ASUS Eee PC 1005HA-PU1X-BK Black Netbook
    • jQuery – Date Picker
    • Using VB.NET From CSharp
    • iTextSharp – Adding Images
    • Hungarian Notation – Use What Works, Spit Out The Bones
    • Pre Order Windows 7
    • jQuery Dialog – With Validation Controls
    • iTextSharp – The easy way
    • Structure of my ASP.NET Web Applications
    • 35% Off Accronis True Image 2009 Home
    • VB.NET Hide Module Name
    • ASP.NET/VB.NET – Video Training
    • Does jQuery Make Us Lazy?
    • PDFs Using iTextSharp
    • Programming SEO – Ping



  • Advertise on this site through Lake Quincy Media
  • DotNetNuke Sponsor

     

    Most Valuable Blogger
  • Sponsor

  • Categories

    • Advanced CSharp
    • Advanced VB.NET
    • ASP.NET MVC
    • Did you know
    • DotNetNuke – Module Development
    • DotNetNuke – Skinning
    • internationalization
    • iTextSharp
    • jQuery
    • none
    • Seach Engine Optimization
    • Silverlight
    • SQL For Programmers
    • Twitter
    • winforms
  • Cloud

    .net ajax architecture asp.net book books containers csharp css dal dataset datasets dotnetnuke events gridview images internationalization internet explorer javascript jQuery json linq listview modules ms-sql MVC objectdatasource programming reflection seo Silverlight skinning sql testing tsql tutorial Twitter twitterizer vb.net video view Vista visual studio webservice WordPress
  • Archives

    • July 2009
    • June 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • September 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • October 2007
  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    • Privacy Policy
  • Calendar

    August 2008
    S M T W T F S
    « Jul   Sep »
     12
    3456789
    10111213141516
    17181920212223
    24252627282930
    31  
  • Blogroll

    • Alvin Ashcraft’s Morning Dew
    • ASP.NET Consulting
    • Life Hacker
    • Remember Anything
    • The Price of Their Toys
    • Uncategorized Thought


.NET Answers © 2007 - 2008 All Rights Reserved.
Entries and Comments.