Windows Phone 应用程序中未找到文件异常 [英] File not found exception in Windows Phone app

查看:50
本文介绍了Windows Phone 应用程序中未找到文件异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序来在 Windows Phone 8.1 RT 中操作 XML.代码如下

I am developing an app to manipulate XML in Windows Phone 8.1 RT. the code is below

private void btnXcute_tapped(object sender, TappedRoutedEventArgs e)
{
    xmlmanipulation();
}

private async void xmlmanipulation()
{
    Random rand = new Random();
    try
    {
        var filex = await ApplicationData.Current.LocalFolder.OpenStreamForReadAsync(@"xmlfile.xml");
        XDocument xdoc = XDocument.Load(filex);
        var word = xdoc.Descendants("word");
        int max = word.Count();
        txt1.Text = word.FirstOrDefault().Value.ToString();
        txt1_Copy.Text = xdoc.Descendants("meaning").FirstOrDefault().Value.ToString();

        //node removal part
        xdoc.Root.Elements("wordset").Where(dim_word => dim_word.Element("word").Value == word.FirstOrDefault().Value.ToString() && dim_word.Element("meaning").Value == xdoc.Descendants("meaning").FirstOrDefault().Value.ToString())
            .FirstOrDefault().Remove();

        //file overwriting part
        var file = await ApplicationData.Current.LocalFolder.CreateFileAsync("xmlfile.xml", CreationCollisionOption.ReplaceExisting);
        using (var stream = await file.OpenStreamForWriteAsync())
        {
            xdoc.Save(stream);      // Save XDocument into the stream
            stream.Position = 0;
        }
    }
    catch (Exception s) 
    {
        txt1.Text = s.Message;
        txt1_Copy.Text = s.Source;

    }
}

我已将 XML 文件 (xmlfile.xml) 放在项目主文件夹中,如下所示

I have placed the XML file(xmlfile.xml) in the project main folder as below

但是我得到 系统找不到指定的文件.(来自 HRESULT:0x80070002 的异常),异常源是 mscorlib.

But I get The system cannot find the file specified. (Exception from HRESULT:0x80070002) and the Exception source is mscorlib.

我对 XML 操作非常陌生.请帮我.我只需要读取一个 XML 文件,修改它的第一个 <wordset> 元素,单击按钮保存文件(覆盖).我总共有 4 个 元素,如果我按 4 次按钮,文件中的所有元素都将被删除.

I am very new to XML manipulation. Please help me. All I need to read an XML file, remode a the very first <wordset> element on it, save the file (overwrite) on a button click. I have total 4 <wordset> elements and if I press the button 4 times, all the elements will be removed in the file.

我的 XML 文件是

<?xml version="1.0" encoding="utf-8" ?>
<xmlfile>
  <wordset>
    <word>word1</word>
    <meaning>meaning1</meaning>
  </wordset>
  <wordset>
    <word>word2</word>
    <meaning>meaning2</meaning>
  </wordset>
  <wordset>
    <word>word3</word>
    <meaning>meaning3</meaning>
  </wordset>
  <wordset>
    <word>word4</word>
    <meaning>meaning4</meaning>
  </wordset>
</xmlfile>

推荐答案

您正在尝试从 安装位置 而非 LocalFolder 访问文件(取决于文件的构建操作).因此你应该使用:

You are trying to access file from Installed location not LocalFolder (depending on file's build action). Therefore you should use:

var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
// not:
var folder = ApplicationData.Current.LocalFolder;

你也可以看看这个答案.

这篇关于Windows Phone 应用程序中未找到文件异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆