我正在尝试开发一个 RSS 阅读器应用程序,并且我想缓存我的 RSS fedd。我要做的是,使用 URL 加载的提要在隔离存储中创建一个 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 文件中读取。当我检查文件是否存在时,它就在那里。但我无法从中读取。谁能告诉我一个解决方案。任何帮助,将不胜感激。谢谢。
更新:我已经更新了上面的代码,我没有收到任何错误。