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

时间:2011-10-24 11:39:03

标签: c# xml windows-phone-7

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

2 个答案:

答案 0 :(得分:3)

让我们将你的任务分成两部分

<强> 1。下载包含图像路径的XML文件

<强> 2。读取该XML文件并将图像控件绑定到该动态路径

让第一个案例的收益:

<强> 1。下载包含图像路径的XML文件

此处路径 = 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文件并将图像控件绑定到动态路径

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

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

答案 1 :(得分:0)

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