My application read stale data from an XML file

I read the data from an XML file as follows:

StringBuilder str = new StringBuilder(); str.Append("<News>"); XDocument xmlDoc = XDocument.Load(path); var q = xmlDoc.Descendants("news") .Where(x => x.Descendants("language_id") != null && x.Descendants("language_id").First().Value == "2") .Select(x => x); foreach (var st in q) { str.Append(st.ToString(SaveOptions.DisableFormatting) + " "); } str.Append("</News>"); return str.ToString(); 

but I recently noticed that when updating the xml file. It still reads data from the old! I do not know if he is reading from a copy or not.

When I reset iis , it updates the data.

How to solve this problem?

+4
source share
2 answers

Quick hack.

Assuming path is a string containing a url, change

 XDocument xmlDoc = XDocument.Load(path); 

to

 XDocument xmlDoc = XDocument.Load(path + "?" + Guid.NewGuid().ToString();); 

This will win any intermediate caching that will continue, but use ? in the url should not violate the server you are requesting for data.

( Guid.NewGuid() is just a quick way to get a random string. You can easily use the Random or DateTime.Now.Ticks class in the same way if you want.)

+3
source

You might need to configure DefaultCachePolicy.

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

From here: http://www.pcreview.co.uk/forums/stop-xmldocument-load-using-cached-data-t3489418.html

+5
source

Source: https://habr.com/ru/post/1432040/


All Articles