读取Xml文件并将内容保存到内存WP7中

我有一个带有数据的xml,在这种情况下,图像存储在互联网上…我想在Windows手机中读取xml并将其保存到内存中……我该怎么做? 任何教程?

让我们将您的任务分为两部分

1.下载包含图像路径的XML文件

2.读取该XML文件并将图像控件绑定到该动态路径

让第一个案例的收益:

1.下载包含图像路径的XML文件

这里Path = http:// server_adrs / XML_FILE

iso_path =隔离存储中的路径,您要保存XML文件。

public void GetXMLFile(string path) { WebClient wcXML = new WebClient(); wcXML.OpenReadAsync(new Uri(path)); wcXML.OpenReadCompleted += new OpenReadCompletedEventHandler(wc); } void wc(object sender, OpenReadCompletedEventArgs e) { var isolatedfile = IsolatedStorageFile.GetUserStoreForApplication(); using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream(iso_path, System.IO.FileMode.Create, isolatedfile)) { byte[] buffer = new byte[e.Result.Length]; while (e.Result.Read(buffer, 0, buffer.Length) > 0) { stream.Write(buffer, 0, buffer.Length); } stream.Flush(); System.Threading.Thread.Sleep(0); } } 

2.读取XML文件并将图像控制绑定到动态路径

这里我有一个显示图像的列表,所以我将按照下面的方式将图像绑定到此列表。

  public IList GetListPerCategory_Icon(string category, string xmlFileName) { using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { if (storage.FileExists(xmlFileName)) { using (Stream stream = storage.OpenFile(xmlFileName, FileMode.Open, FileAccess.Read)) { try { loadedData = XDocument.Load(stream); var data = from query in loadedData.Descendants("category") where query.Element("name").Value == category select new Glossy_Test.Dictionary { Image=GetImage((string)query.Element("iconpress")),//This is a function which will return Bitmap image }; categoryList = data.ToList(); } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK); return categoryList = null; } } } } return categoryList; } 

这里是上述function的定义

  public BitmapImage GetImage(string imagePath) { var image = new BitmapImage(); imagePath = "/Glossy" + imagePath; using (var storage = IsolatedStorageFile.GetUserStoreForApplication()) { if (storage.FileExists(imagePath)) { using (Stream stream = storage.OpenFile(imagePath, FileMode.Open, FileAccess.Read)) { image.SetSource(stream); } } } return image; } 

您可以使用WebClient从服务器中提取xml,然后将其作为XDocument保存在回调中。