Searching XML using a DataSet & DataView

Conclusion

That's all there is to it. For simple uses, this method of storing and retrieving information may be much easier than using a database, however, there are a few things you should keep in mind. First, our simple XML document stores all data as text strings. This isn't too much of a problem since we can use the .NET data types to parse our string data to other types like integer and datetime, but remember when you pass the key value to the Find method that it needs to be a string. Second, if we only want one "row" of data returned for a lookup key, we need to ensure that we don't duplicate the key in our XML document--in a database we could easily enforce this by setting the column to be an identity column.

If you are using this method in a busy ASP.NET application where you have a relatively small amount of data and your data doesn't change too often, you may want to store the DataSet object in cache so the XML document doesn't need to be read and parsed for each request. Use a CacheDependency on the XML file to ensure that the cache is expired when the document is updated.

Download the following for complete code in a console application:

using System ;
using System . Data ;
namespace productsxml {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    classmain     {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static voidMain(string[]args)         {
            //
            // TODO: Add code to start application here
            //
            DataSet dsProducts=newDataSet();
            dsProducts.ReadXml("productlist.xml");
            DataView dvProducts=new
            DataView(dsProducts.Tables["Products"]);
            dvProducts.Sort="SKU";
            introwIndex=dvProducts.Find("1");
            stringPrice,Description;
            if(rowIndex== -1)             {
                // The SKU was not found in our data
                Console.WriteLine("Product Not found");
            }
            else
            {
                Price=dvProducts[rowIndex]["Price"].ToString();
                Console.WriteLine("Price: "+Price);
                // Price: 100.00
                Description=dvProducts[rowIndex]
                ["Description"].ToString();
                Console.WriteLine("Description: "+Description);
                // Description: Widget #1
            }
        }
    }
}

You might also like...

Comments

About the author

Chris Scott

Chris Scott United States

Chris Scott is the founder of Host Orlando (www.hostorlando.com) a Web hosting and development company in Orlando, FL specializing in Microsoft technologies. A self-taught programmer, Chris sta...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The greatest performance improvement of all is when a system goes from not-working to working.” - John Ousterhout