Stop XmlDocument.Load() from using cached data?

  • Thread starter Mahmoud Al-Qudsi
  • Start date
M

Mahmoud Al-Qudsi

Is there any way to stop an XmlDocument object from using data grabbed
from a previous request?

e.g. if I used XmlDocument.Load to grab a URI, and I know that this
URI changes often (for example, an RSS feed) can I somehow tell the
framework to not use cached data?

Will I have to create a custom HttpWebRequest and convert the data to
an XmlDocument for processing or is there some way to tell XmlDocument
(perhaps via the XmlResolver somehow?) to never use the cache?

Thanks.
 
A

Anthony Jones

Mahmoud Al-Qudsi said:
Is there any way to stop an XmlDocument object from using data grabbed
from a previous request?

e.g. if I used XmlDocument.Load to grab a URI, and I know that this
URI changes often (for example, an RSS feed) can I somehow tell the
framework to not use cached data?

Will I have to create a custom HttpWebRequest and convert the data to
an XmlDocument for processing or is there some way to tell XmlDocument
(perhaps via the XmlResolver somehow?) to never use the cache?

The Load method honors the WebRequest Caching policy which by default is set
to bypass the cache. However its worth noting that this actually means
bypass the local cache it does not add a pragma: no-cache header to the
request and therefore other caches such as a proxy server between the client
and the origin server may serve content from a cache.

You will need to use HttpWebRequest if the default behaviour (or your
prevailing settings set via the requestCaching element in the .config(s)) is
not want you need.

I suggest you use a policy created wth Revalidate level.
 
M

Mahmoud Al-Qudsi

You will need to use HttpWebRequest if the default behaviour (or your
prevailing settings set via the requestCaching element in the .config(s)) is
not want you need.

I suggest you use a policy created wth Revalidate level.

I take it this will this do the trick then?

.....
WebRequest.DefaultCachePolicy = new
RequestCachePolicy(RequestCacheLevel.Revalidate);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(myUri);
 
A

Anthony Jones

Mahmoud Al-Qudsi said:
I take it this will this do the trick then?

....
WebRequest.DefaultCachePolicy = new
RequestCachePolicy(RequestCacheLevel.Revalidate);
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(myUri);

Assuming you don't have a conflicting setting in the application's config(s)
then yes that should do it.
 
A

Anthony Jones

Mahmoud Al-Qudsi said:
Thanks, it worked great.

Perhaps I should have mentioned in response to your previous reply that
modifying the DefaultCachePolicy as you are doing there affects _all_ Web
Client code. This may have some un-wanted side effects in previously
working code.

Since any code in the app domain can muck about with the DefaultCachePolicy
its actually not a good idea to change it or rely on it. I think the best
approach is to use an instance of HttpWebRequest and set the policy of that
instance.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top