Click here to Skip to main content
13,554,524 members
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 15 Jun 2008
Licenced CPOL

Hack to enforce the cache of an XmlDataSource to invalidate

, 15 Jun 2008
Rate this:
Please Sign up or sign in to vote.
I found that when you change the Data property of an XmlDataSource control via code, it will not automatically invalidate the cache. This is a hack for this problem.


We often use the XmlDataSource control together with the TreeView control.

If the data source is static, it works quite well. But, when the data source needs to be dynamic, there seems to be some problems. This article will introduce one such problem and give you a hack solution.


If the data source needs to be dynamic, you may choose to:

  1. Put several XmlDataSource controls on the web form, and switch it by setting different DataSourceIDs.
  2. Put only one XmlDataSource control and change the value of the DataFile or Data property of it, when necessary.

Obviously, the first way is more limited and only applicable for some special situations. It's not a good idea to put a lot of data source controls on one form and, more importantly, the count of the data source controls must be a fixed number.

But, what will happen when you change the value of the DataFile or Data property of the XmlDataSource control? From Microsoft's documentations, you may find the following information:

If you change the value of the DataFile/Data property, the DataSourceChanged event is raised. If caching is enabled and you change the value of DataFile/Data, the cache is invalidated.

Unfortunately, in my test (you may download the samples to see it yourself), I found that, the cache will not be automatically invalidated when you change the value of the Data property of the XmlDataSource control, although DataFile makes no trouble.


First, you may choose to change the value of the DataFile property of the XmlDataSource, instead of Data. This is an easy solution.

Otherwise, if you stick to changing the property of Data for some reason (for example, your data source is not a real XML file, but just a dynamically-populated string), you may try the following hack method:

public static void XmlDataSourceCacheHack(XmlDataSource dataSource)
        Type t = typeof(XmlDataSource);
        MethodInfo m = t.GetMethod("CreateCacheKey", 
                       BindingFlags.Instance | BindingFlags.NonPublic);
        string key = (string)m.Invoke(dataSource, null);
        PropertyInfo p = t.GetProperty("Cache", 
                         BindingFlags.Instance | BindingFlags.NonPublic);
        object cache = p.GetValue(dataSource, null);

        Type t2 = t.Assembly.GetType("System.Web.UI.DataSourceCache");
        MethodInfo m2 = t2.GetMethod("Invalidate", 
                        BindingFlags.Instance | BindingFlags.Public);
        m2.Invoke(cache, new object[] { key });

(It's sure that "using System.Reflection;" is required at the beginning of the file.)

This is just a hack for this problem. I spent more than one day on debugging to find the source of this strange problem.

(P.S.: If you find a more perfect way to solve it, avoiding Reflection, do tell me. Thanks!)

Thanks for PlamenLeykov

You may surely entirely disable the cache feature of the XmlDataSource. It also works fine. This is another easy solution.

But, if your situation is just like mine: the change possibilities are lower than other postback possibilities (for example, the TreeView is quite big and always triggers postbacks more frequently than data sources change), you will prefer using Reflection than disabling the cache.

Is it a bug from Microsoft?

I'm not sure whether this is a bug from Microsoft, or if it is just applied to the design specification. But, either the implementation is wrong, or the documentation is wrong.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
China China
I am a Chinese software developer, while I am now working in Japan.
If you can read in Chinese, you may visit my blog in Chinese:

You may also be interested in...


Comments and Discussions

GeneralA better solution Pin
MatesL15-Jun-09 2:09
memberMatesL15-Jun-09 2:09 
GeneralRe: A better solution Pin
tomlev10-Nov-09 2:57
membertomlev10-Nov-09 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180515.1 | Last Updated 16 Jun 2008
Article Copyright 2008 by percyboy
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid