vish213
vish213

Reputation: 786

Xdocument.load( ) does not refreshes with xml file update

I am trying to build a small desktop scoreboard which can get and show live matches score in C#. Now when I call Xdocument.load(url) method on button click, it loads latest scores for the first time only, then keep showing the same score even though Xdocument.load() is defined to run on button click(as well as when Form loads,but I don't think that will make a difference). Now what I have found on google and stackoverflow is something like to see when file is updated,and related stuff. But all those are for local xml files. How do I solve my problem?

     public Form1()
    {
        InitializeComponent();
       // WebRequest.DefaultCachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate);   
        feed = XDocument.Load(url);


    }

    private void button1_Click(object sender, EventArgs e)
    {
        feed = XDocument.Load(url + "?_=" + Guid.NewGuid().ToString());
         feed=XDocument.Load(url);
       // var nofmlive = feed.Root.Element("match").Attribute("srs");
        var output = GetItems();
        foreach (var v in output) {
       //rest of the code,,just to get and show data of all matches which are currently live

Upvotes: 1

Views: 1422

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038990

Maybe XDocument is caching the data. Try busting the cache by appending a Guid query string parameter parameter:

var doc = XDocument.Load(url + "?_=" + Guid.NewGuid().ToString());

Adjust the ? with & if you already have query string parameters in the url.

You might also try changing the cache policy but the drawback of this approach is that it is global to all HTTP requests:

WebRequest.DefaultCachePolicy = new RequestCachePolicy(RequestCacheLevel.Revalidate);
var doc = XDocument.Load(url);

Alternatively you may try loading the XML with a WebClient and then feeding it to the XDocument:

using (var client = new WebClient())
{
    string xml = client.DownloadString("http://synd.cricbuzz.com/j2me/1.0/livematches.xml?_=" + Guid.NewGuid().ToString());
    var doc = XDocument.Parse(xml);
}

Upvotes: 4

Related Questions