繁体   English   中英

Windows Phone应用程序中找不到文件异常

[英]File not found exception in Windows Phone app

我正在开发一个在Windows Phone 8.1 RT中处理XML的应用程序。 代码在下面

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)放置在项目主文件夹中,如下所示 在此处输入图片说明

但是我得到The system cannot find the file specified. (Exception from HRESULT:0x80070002) The system cannot find the file specified. (Exception from HRESULT:0x80070002)的异常The system cannot find the file specified. (Exception from HRESULT:0x80070002) ,并且异常源是mscorlib

我对XML操作非常陌生。 请帮我。 我需要读取一个XML文件,重新对其上的第一个<wordset>元素进行重新设置,单击按钮保存该文件(覆盖)。 我总共有4个<wordset>元素,如果按4次按钮,所有元素都将在文件中删除。

我的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访问文件(取决于文件的生成操作)。 因此,您应该使用:

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

您也可以看看这个答案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM