【问题标题】:Read Xml file and save content into memory WP7读取 Xml 文件并将内容保存到内存中 WP7
【发布时间】:2011-10-24 21:21:49
【问题描述】:

我有一个带有数据的 xml,在这种情况下,图像存储在互联网中。我想在 windows phone 中读取 xml 并将其保存到内存中。我该怎么做?有教程吗?

【问题讨论】:

  • 您从哪里读取/获取文件?你想把它保存在哪里?内存?
  • 我想从服务器读取文件,我想保存在内存或存储卡中..

标签: c# xml windows-phone-7


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-06
      相关资源
      最近更新 更多