0

我正在从 Internet 读取 XML 文件。我正在将它写入隔离存储,然后我喜欢阅读它。这是代码

  IsolatedStorageFile isf=IsolatedStorageFile.GetUserStoreForApplication();
    private void findNearestButton_Click(object sender, RoutedEventArgs e)
    {

        findCity(isf);
    }

  private void findCity(IsolatedStorageFile isf)
    {
       filePath = AppResource.exchangeOfficesFile;

            if (!isf.FileExists(filePath.ToString()))
            {
                takeXMLOnLine(isf,filePath);
            }
            parseXMLfile(isf,filePath);

    }



 private void takeXMLOnLine(IsolatedStorageFile isf, string filePath)
    {
        System.Uri targetUri = new System.Uri(AppResource.exchangeOfficesURI);
        WebClient client = new WebClient();
        client.DownloadStringAsync(targetUri);
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

    }



void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath, FileMode.Create))
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.ConformanceLevel = ConformanceLevel.Auto;
            using (XmlWriter writer = XmlWriter.Create(rawStream, settings))
            {
                string xmlResponse = e.Result.ToString();
                xmlResponse = xmlResponse.Replace("&lt;", "<");
                xmlResponse = xmlResponse.Replace("&gt;", ">");
                writer.WriteString(xmlResponse);
                // Write the XML to the file.
                writer.Flush();
            }

        }
    }


 private void parseXMLfile(IsolatedStorageFile isf, string filePath)
    {

            using (IsolatedStorageFileStream str = isf.OpenFile(filePath, FileMode.Open, FileAccess.Read))
            {
                StreamReader reader = new StreamReader(str);
                string line;


                while ((line = reader.ReadLine()) != null)
                {

                    MessageBox.Show(line);


                }
                reader.Close();
            }

}

当我运行代码时,在此行出现 Operation not allow on IsolatedStoreageFileStream 错误

  using (IsolatedStorageFileStream str = isf.OpenFile(filePath, FileMode.Open, FileAccess.Read))

有人可以帮我吗?

4

3 回答 3

0

如果文件路径有子目录,查看该目录是否存在。这可能会导致这个问题。

于 2012-06-09T01:34:08.260 回答
0

尝试这样的事情:

using (IsolatedStorageFile rootStore = IsolatedStorageFile.GetUserStoreForApplication())
{
    using (IsolatedStorageFileStream fs = new IsolatedStorageFileStream(filePath,
                    System.IO.FileMode.Open, rootStore))
    {
        ... whatever you do with the file goes here
    }
}
于 2012-06-09T00:58:15.720 回答
0

当您尝试读取文件时该文件是否存在?看起来如果文件不存在,您开始下载过程(但不要等待完成)然后尝试读取它。

所以取而代之的是:

private void findCity(IsolatedStorageFile isf)
{
    filePath = AppResource.exchangeOfficesFile;

    if (!isf.FileExists(filePath.ToString()))
    {
        takeXMLOnLine(isf,filePath);
    }
    else
    {
        parseXMLfile(isf,filePath);
    }
}

void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    using (IsolatedStorageFileStream rawStream = isf.OpenFile(filePath, FileMode.Create))
    {
        XmlWriterSettings settings = new XmlWriterSettings();
        settings.ConformanceLevel = ConformanceLevel.Auto;
        using (XmlWriter writer = XmlWriter.Create(rawStream, settings))
        {
            string xmlResponse = e.Result.ToString();
            xmlResponse = xmlResponse.Replace("&lt;", "<");
            xmlResponse = xmlResponse.Replace("&gt;", ">");
            writer.WriteString(xmlResponse);
            // Write the XML to the file.
            writer.Flush();
        }
    }

    parseXMLfile(isf,filePath);
}
于 2012-06-10T15:44:44.557 回答