2011-10-24 152 views
0

我有一个XML数据,在这种情况下,图像存储在互联网..我想读取Windows手机中的XML并将其保存到内存..我该怎么办那?任何教程?阅读Xml文件并将内容保存到内存中WP7

+0

你在哪里读/定义从得到的文件?你想在哪里保存它?内部存储器? –

+0

我想从服务器读取文件,我想保存在内部存储器或存储卡.. – jpmd

回答

3

允许将您的任务分为两个部分

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

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

让第一种情况收益:

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

这里路径 = HTTP:// server_adrs/XML_FILE

iso_path =隔离储存其中u要保存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<Dictionary> 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; 
    } 

,并在这里对上述功能

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

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

相关问题