C# 将从web服务接收的数据存储在windows phone 7应用程序中的独立存储中
我正在为WindowsPhone7构建一个应用程序,需要从web服务中提取数据并在应用程序中显示。现在,每当数据来自web服务的页面被调用时,web服务就会被一次又一次地调用,因此显示数据需要时间。所以我想将数据存储在独立的存储器中,这样web服务就不会每次都被调用。请帮我做这个。调用web服务的代码是:C# 将从web服务接收的数据存储在windows phone 7应用程序中的独立存储中,c#,web-services,windows-phone-7,C#,Web Services,Windows Phone 7,我正在为WindowsPhone7构建一个应用程序,需要从web服务中提取数据并在应用程序中显示。现在,每当数据来自web服务的页面被调用时,web服务就会被一次又一次地调用,因此显示数据需要时间。所以我想将数据存储在独立的存储器中,这样web服务就不会每次都被调用。请帮我做这个。调用web服务的代码是: public const string aboutxml = "about.xml"; public about() { InitializeComponen
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;
}
public const string aboutxml=“about.xml”;
公众关于()
{
初始化组件();
LoadData();
}
私有void LoadData()
{
bool-issucess;
//尝试从iso存储加载数据
var doc=ReadXml(out issucess);
if(isSuccess)大众列表(doc);
//如果失败(iso存储中不存在数据),请从web服务下载数据
其他的
{
KejriwalService.aapSoapClient myclient=新的KejriwalService.aapSoapClient();
myclient.getarvindAboutCompleted+=新事件处理程序(myclient_getarvindAboutCompleted);
myclient.getarvindAboutAsync();
progressName.Visibility=System.Windows.Visibility.Visible;
}
}
//下载完成后,显示数据,然后将xml保存到iso存储
void myclient_getarvindAboutCompleted(对象发送方,KejriwalService.getarvindAboutCompletedEventArgs e)
{
var doc=XDocument.Parse(e.Result);
大众主义者(doc);
WriteXml(doc);
}
私有作废公众列表(XDocument doc)
{
var数据=e.结果;
XElement xml=XElement.Parse(数据);
aboutview.Text=xml.Elements(“UserDetails”).Elements(“about_details”).First().Value;
progressName.Visibility=System.Windows.Visibility.Collapsed;
}
私有XDocument ReadXml(out bool isSuccess)
{
isSuccess=false;
var doc=新的XDocument();
使用(IsolatedStorageFile store=IsolatedStorageFile.GetUserStoreForApplication())
{
尝试
{
if(store.FileExists(aboutxml))
{
使用(var sr=new StreamReader(新的隔离存储文件流(aboutxml,FileMode.OpenOrCreate,store)))
{
doc=XDocument.Load(sr);
}
isSuccess=true;
}
}
捕获(例外情况除外){}
}
退货单;
}
私有bool WriteXml(XDocument文档)
{
使用(IsolatedStorageFile store=IsolatedStorageFile.GetUserStoreForApplication())
{
尝试
{
使用(var sw=newstreamwriter(新的隔离存储文件流(aboutxml,FileMode.Create,store)))
{
sw.Write(document.ToString());
}
}
catch(异常ex){return false;}
}
返回true;
}
将所有数据存储在XDocument中并进行检索是一项开销。而是使用数据库来存储和检索数据。这将减少您的代码工作 好的,我很清楚您需要一些代码,所以我们开始吧,尝试以下代码:
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;
}
公共常量字符串NewssXml=“Newss.xml”;
公共新闻
{
初始化组件();
LoadData();
}
私有void LoadData()
{
bool-issucess;
//尝试从iso存储加载数据
var doc=ReadXml(out issucess);
if(isSuccess)大众列表(doc);
//如果失败(iso存储中不存在数据),请从web服务下载数据
其他的
{
KejriwalService.aapSoapClient client=新的KejriwalService.aapSoapClient();
client.getarvindNewsCompleted+=新事件处理程序(client_getarvindNewsCompleted);
getarvindNewsAsync();
progressName.Visibility=System.Windows.Visibility.Visible;
}
}
//下载完成后,显示数据,然后将xml保存到iso存储
无效客户端\u getarvindNewsCompleted(对象发送方,KejriwalService.getarvindNewsCompletedEventArgs e)
{
var doc=XDocument.Parse(e.Result);
大众主义者(doc);
WriteXml(doc);
}
私有作废公众列表(XDocument doc)
{
List listData=新列表();
progressName.Visibility=System.Windows.Visibility.Collapsed;
foreach(文档子体(“UserDetails”)中的变量位置)
{
新闻数据=新新闻();
data.News\u Title=location.Element(“News\u Title”).值;
data.News\u Description=location.Element(“News\u Description”).值;
data.Date\u Start=location.Element(“Date\u Start”).值;
data.image\u path=location.Element(“image\u path”).Value;
data.ImageBind=新的位图图像(新的Uri(@)http://political-leader.vzons.com/ArvindKejriwal/images/uploaded/“+data.image_path,UriKind.Absolute”);
添加(数据);
}
listBox1.ItemsSource=listData;
}
私有XDocument ReadXml(out bool isSuccess)
{
isSuccess=false;
var doc=新的XDocument();
使用(IsolatedStorageFile store=IsolatedStorageFile.GetUserStoreForApplication())
{
尝试
{
if(store.FileExists(newsxml))
{
使用(var sr=newstreamreader(新的隔离存储文件流(NewssXml,FileMode.OpenOrCreate,store)))
{
doc=XDocument.Load(sr);
}
isSuccess=true;
}
}捕获(例外情况除外){}
}
退货单;
}
私有bool WriteXml(XDocument文档)
{
使用(IsolatedStorageFile store=IsolatedStorageFile.GetUserStoreForApplication())
{
尝试
{
使用(var sw=newstreamwriter(新的隔离存储文件流(NewssXml,FileMode.Create,store)))
{
sw.Write(document.ToString());
}
}catch(异常ex){return false;}