读取XML文件并保存内容到内存WP7 [英] Read Xml file and save content into memory WP7

查看:184
本文介绍了读取XML文件并保存内容到内存WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有数据的XML,存储在internet..i这种情况下,图像要读取的windows phone的XML并将其保存到存储..我该怎么办呢?任何教程?

I have a xml with data, in this case images stored in the internet..i want to read the xml in windows phone and save it to the memory.. how can i do that? any tutorial?

推荐答案

让我们将您的任务分成两部分。

Lets divide your task into two parts

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

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

让我们从第一种情况下进行:

Lets Proceeds with first case:

1。下载包含映像路径

下面的XML文件的路径 = HTTP:// server_adrs / XML_FILE

here Path=http://server_adrs/XML_FILE

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

iso_path=Path inside Isolated Storage where u want to save XML file.

    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文件和

在这里我有这是否显示图像的列表图像控件绑定到动态路径,所以我将一个函数绑定图像。这个列表按照以下

here i am having an List which is showing an images, so i will a function to bind images to this list as per below.

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

和这里的定义为上述功能

and here the definition for above 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;
    }

这篇关于读取XML文件并保存内容到内存WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆