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;
}