Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 33,393 times

Contents

Related Categories

Searching XML using a DataSet & DataView - Conclusion

cjscott69

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

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 started getting serious about coding three years ago and learned ASP, T-SQL, and JavaScript to supplement his experience with Visual Basic, databases, and shell scripting. He is now focusing on writing Web applications in ASP.NET and is currently working on finishing a transition of his company's customer management system from classic ASP to ASP.NET.

Comments