如何使用现有流在WP7中编写xml

时间:2012-12-30 11:44:57

标签: .net rss windows-phone rss-reader isolatedstorage

我正在尝试开发一个RSS阅读器应用程序,我想缓存我的RSS。我要做的是,使用URL加载的feed在隔离存储中创建XML文件。这是我到目前为止所做的。

// Constructor
    public MainPage()
    {
        InitializeComponent();
        using (IsolatedStorageFile isstorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isstorage.FileExists(rssCacheFile))
            {
                MessageBox.Show("Reading from cache");
                readCache(rssCacheFile);
            }
            else
            {
                MessageBox.Show("Reading from web");
                readFeed(rssLink);
            }
        }
    }

    public void readFeed(string link) 
    {
        WebClient wc = new WebClient();
        wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
        wc.OpenReadAsync(new Uri(link, UriKind.Absolute));
    }

    void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {

        Stream rssStream = e.Result;
        XmlReader response = XmlReader.Create(rssStream);
        SyndicationFeed feeds = SyndicationFeed.Load(response);

        foreach (SyndicationItem f in feeds.Items) 
        {
            itemList.Add(new RssItem {Title = f.Title.Text });
        }

        listBox1.ItemsSource = itemList;

        //Write to the cache
        writeXML(rssStream);
    }

    public void writeXML(Stream rssStream) 
    {
        using (IsolatedStorageFile isstorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream isstream = new IsolatedStorageFileStream(rssCacheFile, FileMode.Create, FileAccess.Write, isstorage))
            {
                byte[] buffer = new byte[rssStream.Length];
                while (rssStream.Read(buffer, 0, buffer.Length) > 0)
                {
                    isstream.Write(buffer, 0, buffer.Length);
                }

                isstream.Flush();
                System.Threading.Thread.Sleep(0);
            }
        }
    }

    public void readCache(string fileName) 
    {
        //IsolatedStorageFile isstorage = IsolatedStorageFile.GetUserStoreForApplication();
        using (IsolatedStorageFile isstorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            if (isstorage.FileExists(fileName))
            {
                try
                {
                    using (isstorage)
                    {
                        IsolatedStorageFileStream stream = isstorage.OpenFile(fileName, FileMode.Open, FileAccess.Read);
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            rssXml.Text = reader.ReadToEnd();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else 
            {
                MessageBox.Show("Cache is empty....");
            }
        }
    }

但问题是,我无法从写入的XML文件中读取。当我检查文件是否存在时,它就在那里。但我无法从中读到。任何人都可以告诉我一个解决方案。任何帮助,将不胜感激。谢谢。

更新:我已更新上述代码,但我没有收到任何错误。

1 个答案:

答案 0 :(得分:0)

你不应该将你的isstorage变量放在using中,因为它会在使用结束时处理它,而这不是你想要的类变量。还要将您的流变量放在readCache中使用,以便它关闭并处理流。

另外,请确保查看您获得的例外情况,以便我们提供更好的帮助。