0

我喜欢从 Internet 下载 XML 文件并将其写入到 IsolatedStorage 文件中。这是我正在使用的代码

public StreamReader readerForXml;

 private void find()
    {
       string filePath = "myFile.xml";
        using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
        {
    using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("People2.xml", FileMode.Create, myIsolatedStorage))
{



            XmlWriterSettings settings = new XmlWriterSettings();
             settings.Indent = true;
             using (XmlWriter writer = XmlWriter.Create(rawStream, settings))
             {
                 System.Uri targetUri = new System.Uri("http://dl.dropbox.com/u/29465448/ExchangeOffices.xml");
                 WebClient client = new WebClient();
                 try
                 {
                     client.DownloadStringAsync(targetUri);
                     client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);




writer.WriteString(sReader.ToString());
                    writer.Flush();
                }
                catch (Exception exception)
                {
                     MessageBox.Show(exception.Message);
                 }

}

} }

 void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        sReader = new StreamReader(e.Result.ToString());

}

我得到 StreamReader sReader 的 NullReferenceException,有人有同样的问题吗?谢谢

4

1 回答 1

0

您已经将 xml 数据作为字符串保存,因此您无需读取它,只需将其写入到 IsolatedStorage。我最近做了同样的事情并做了一个小助手功能。您可以从 调用它client_DownloadStringCompleted,传递它e.result"myFile.xml"

希望这可以帮助。

    public void SaveXML(string xmldata, string filename)
    {
        using (var isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (var isoStream = new IsolatedStorageFileStream(filename, FileMode.Create, isolatedStorage))
            {
                var settings = new XmlWriterSettings();
                settings.ConformanceLevel = ConformanceLevel.Auto;
                using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
                {
                    writer.WriteString(xmldata);
                    // Write the XML to the file.
                    writer.Flush();
                }
            }
        }
    }
于 2012-06-07T22:22:26.767 回答