tags:

views:

78

answers:

1

I need to create a cache using an XML file. Here's the signature of the method that I will be using. I want this method to return the id based on the key-product_name. So I want it to create a cache one time programmatically and then only if the key is not found then add that.

public static string getProductId(string product_name)
    public static string getTechId(string fieldName)
    {
        Cache cache = HttpContext.Current.Cache;  //neeed to change this.
        string cacheNameEpm = product_name + "TechName";

        if (cache[cacheNameEpm] == null)
        {
            XPathDocument doc = new XPathDocument(HttpContext.Current.Request.MapPath("inc/xml/prd.xml"));
            XPathNavigator navigator = doc.CreateNavigator();
            string selectName = "/Products/Product[ProductName ='" + fieldName + "']/ProductId";
            XPathNodeIterator nodes = navigator.Select(selectName);

            if (nodes.Count > 0)
            {
                nodes.MoveNext();
                cache.Add(cacheNameEpm, nodes.Current.Value, null, DateTime.Now + new TimeSpan(4, 0, 0), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
            }
        }
        return cache[cacheNameEpm] as string;
    }

Here is the xml file:

<Products>
    <Product>
        <ProductName>PDPArch</ProductName>
        <ProductId>57947</ProductId>
    </Product>
    <Product>
        <ProductName>TYFTType</ProductName>
        <ProductId>94384</ProductId>
    </Product>
</Products>
A: 

Why not just read the entire XML file in and add it into a dictionary? You can stick the entire dictionary into the cache and leave it there.

Steven Sudit