简体   繁体   中英

storing data received from web service in isolated storage in windows phone 7 application

I am building an app for windows phone 7 where i need to extract data from web service and display it in my application. Now everytime the page where the data comes from web service is called the web servie is called again and again and as a result it takes time for the data to be displayed. So i want to store the data in isolated storage so that the web service is not called everytime. Please help me to do this. My code where the web service is called is:

public const string aboutxml = "about.xml";

    public about()
    {
        InitializeComponent();
        LoadData();
    }

    private void LoadData()
    {
        bool isSuccess;
        //try to load data from iso store
        var doc = ReadXml(out isSuccess);
        if (isSuccess) PopulateList(doc);
        //if failed (data doesn't exists in iso store), download data from web service
        else
        {
            KejriwalService.aapSoapClient myclient = new KejriwalService.aapSoapClient();
            myclient.getarvindAboutCompleted += new EventHandler<KejriwalService.getarvindAboutCompletedEventArgs>(myclient_getarvindAboutCompleted);
            myclient.getarvindAboutAsync();
            progressName.Visibility = System.Windows.Visibility.Visible;

        }
    }

    //upon download completed, display data then save the xml to iso store
    void myclient_getarvindAboutCompleted(object sender, KejriwalService.getarvindAboutCompletedEventArgs e)
    {
        var doc = XDocument.Parse(e.Result);
        PopulateList(doc);
        WriteXml(doc);
    }


    private void PopulateList(XDocument doc)
    {
          var data = e.Result;

        XElement xml = XElement.Parse(data);

        aboutview.Text = xml.Elements("UserDetails").Elements("about_details").First().Value;
        progressName.Visibility = System.Windows.Visibility.Collapsed;

    }

    private XDocument ReadXml(out bool isSuccess)
    {
        isSuccess = false;
        var doc = new XDocument();
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            try
            {
                if (store.FileExists(aboutxml))
                {
                    using (var sr = new StreamReader(new IsolatedStorageFileStream(aboutxml, FileMode.OpenOrCreate, store)))
                    {
                        doc = XDocument.Load(sr);
                    }
                    isSuccess = true;
                }
            }
            catch (Exception ex) { }
        }
        return doc;
    }

    private bool WriteXml(XDocument document)
    {
        using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
        {
            try
            {
                using (var sw = new StreamWriter(new IsolatedStorageFileStream(aboutxml, FileMode.Create, store)))
                {
                    sw.Write(document.ToString());
                }
            }
            catch (Exception ex) { return false; }
        }
        return true;
    }

Ok, I know very well you want some codes so here we go, try following codes :

public const string NewssXml = "Newss.xml";

public News()
{
    InitializeComponent();
    LoadData();
}

private void LoadData()
{
    bool isSuccess;
    //try to load data from iso store
    var doc = ReadXml(out isSuccess);
    if(isSuccess) PopulateList(doc);
    //if failed (data doesn't exists in iso store), download data from web service
    else 
    {
        KejriwalService.aapSoapClient client = new KejriwalService.aapSoapClient();
        client.getarvindNewsCompleted += new EventHandler<KejriwalService.getarvindNewsCompletedEventArgs>(client_getarvindNewsCompleted);
        client.getarvindNewsAsync();

        progressName.Visibility = System.Windows.Visibility.Visible;
    }
}

//upon download completed, display data then save the xml to iso store
void client_getarvindNewsCompleted(object sender, KejriwalService.getarvindNewsCompletedEventArgs e)
{
    var doc = XDocument.Parse(e.Result);
    PopulateList(doc);
    WriteXml(doc);
}

private void PopulateList(XDocument doc)
{
    List<Newss> listData = new List<Newss>();

    progressName.Visibility = System.Windows.Visibility.Collapsed;

    foreach (var location in doc.Descendants("UserDetails"))
    {
        Newss data = new Newss();
        data.News_Title = location.Element("News_Title").Value;
        data.News_Description = location.Element("News_Description").Value;
        data.Date_Start = location.Element("Date_Start").Value;
        data.image_path = location.Element("image_path").Value;
        data.ImageBind = new BitmapImage(new Uri( @"http://political-leader.vzons.com/ArvindKejriwal/images/uploaded/"+data.image_path, UriKind.Absolute));
        listData.Add(data);
    }
    listBox1.ItemsSource = listData;
}

private XDocument ReadXml(out bool isSuccess) 
{ 
    isSuccess = false;
    var doc = new XDocument(); 
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
        try 
        { 
            if (store.FileExists(NewssXml)) 
            { 
                using (var sr = new StreamReader(new IsolatedStorageFileStream(NewssXml, FileMode.OpenOrCreate, store))) 
                { 
                    doc = XDocument.Load(sr); 
                }
                isSuccess = true;               
            } 
        } catch (Exception ex) { } 
    } 
    return doc; 
}

private bool WriteXml(XDocument document) 
{ 
    using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication()) 
    { 
        try 
        { 
            using (var sw = new StreamWriter(new IsolatedStorageFileStream(NewssXml, FileMode.Create, store))) 
            { 
                sw.Write(document.ToString()); 
            } 
        } catch (Exception ex) { return false; } 
    } 
    return true; 
}

Only if you care to know how this built :

  • General steps are the same as I explained about last month in this answer .
  • ReadXml and WriteXml method adapted from this blog post
  • The rest is refactored from your existing code

Storing all the data in XDocument and retrieving is overhead. Instead use your database to store and retrieve the data. This would reduce your code work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM