Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 读取Xml文件并将内容保存到内存WP7中_C#_Xml_Windows Phone 7 - Fatal编程技术网

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

C# 读取Xml文件并将内容保存到内存WP7中,c#,xml,windows-phone-7,C#,Xml,Windows Phone 7,我有一个包含数据的xml,在本例中是存储在internet中的图像。我想在windows phone中读取xml并将其保存到内存中。。我该怎么做?任何教程?您可以使用WebClient从服务器提取xml,然后将其保存为回调中的XDocument。您可以使用WebClient从服务器提取xml,然后将其保存为回调中的XDocument。让我们将任务分为两部分 1。正在下载包含图像路径的XML文件 2。读取该XML文件并将图像控件绑定到该动态路径 让我们继续处理第一种情况: 1。正在下载包含图像路径

我有一个包含数据的xml,在本例中是存储在internet中的图像。我想在windows phone中读取xml并将其保存到内存中。。我该怎么做?任何教程?

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

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

让我们将任务分为两部分

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

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

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

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