在通用应用程序中使用 c# 保存 xml 文件

save a xml file with c# in Universal app

我有这个 Xml :

<?xml version="1.0" encoding="utf-8" ?>
<GirlsTimes>
  <Angie>00:00:00</Angie>
  ...
  <Nicole>00:00:00</Nicole>
</GirlsTimes>

我有一个文本框,您可以输入 "hh:mm:ss" 格式的时间 (TBAngie)。 当此文本框上的焦点丢失时,我想将新值保存在同一个 Xml 文件中:

private async void TBAngie_LostFocus(object sender, RoutedEventArgs e)
{
    try
    {
        if (!checkContent(TBAngie.Text))
        {
            TBAngie.Text = string.Empty;
            var dialog = new MessageDialog("Veuillez encodez un temps hh:mm:ss svp.");
            await dialog.ShowAsync();
        }
        else
        {
            StringBuilder sb = new StringBuilder();
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.OmitXmlDeclaration = true;
            xws.Indent = true;

            using (XmlWriter xw = XmlWriter.Create(sb, xws))
            {
                string repMaxXMLPath = Path.Combine(Package.Current.InstalledLocation.Path, "XML/GirlsTimes.xml");
                loadedData = XDocument.Load(repMaxXMLPath);
                var items = from item in loadedData.Descendants("GirlsTimes")
                            where item.Element("Angie").Value != TBAngie.Text
                            select item;

                foreach (XElement itemElement in items)
                {
                    itemElement.SetElementValue("Angie", TBAngie.Text);
                }

                loadedData.Save(xw);
            }
        }
    }
    catch (Exception ex)
    {
        var dialog = new MessageDialog(ex.Message);
        await dialog.ShowAsync();
    }
}

显然 <Angie> 节点值更新良好(我在调试模式下看到更新的节点),但是当我刷新页面(退出当前页面并重新加载)时,该值没有更新。所以我认为我保存新xml的方法不好,但我不知道为什么...

我哪里做错了?提前致谢。

编辑

就在 loadedData.Save() 之前,我做了一个 .toString() 并且我有这个 :

<GirlsTimes>
  <Angie>00:12:34</Angie>
   ...
  <Nicole>00:00:00</Nicole>
</GirlsTimes>

应用程序的安装目录是只读位置。如果您需要更改文件内容,请将其复制到 Windows.Storage.ApplicationData.Current.LocalFolder。因此,当您的应用加载文件时,首先尝试 LocalFolder 位置,如果没有文件,则从 InstalledLocation.

读取它

详见this article

Wouaouhhh,经过多次搜索和尝试,我找到了解决方案:

这是我的阅读方法(我知道这不是正确的使用方法 try/catch/finally...)):

        private async void GetGirlsTimes()
        {
            StorageFolder localFolder = ApplicationData.Current.LocalFolder;
            try
            {
                StorageFile textFile = await localFolder.GetFileAsync("GirlsTimes.xml");
                using (IRandomAccessStream textStream = await textFile.OpenReadAsync())
                {
                    Stream s = textStream.AsStreamForRead();
                    loadedData = XDocument.Load(s);
                }
            }
            catch
            {
                storageFolder = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("AppData");
                storageFile = await storageFolder.GetFileAsync("GirlsTimes.xml");
                using (IRandomAccessStream writeStream = await storageFile.OpenAsync(FileAccessMode.Read))
                {
                    Stream s = writeStream.AsStreamForRead();
                    loadedData = XDocument.Load(s);
                }
            }
            finally
            {
               ...//put the value from loadedData to my differents TextBoxes.
            }
        }

这是我的写入方法:

 var items = from item in loadedData.Descendants("GirlsTimes")
                                where item.Element("Angie").Name == "Angie"
                                select item;

                    foreach (XElement itemElement in items)
                    {
                        itemElement.SetElementValue("Angie", TBAngie.Text);
                    }

                    StorageFolder localFolder = ApplicationData.Current.LocalFolder;
                    StorageFile textFile = await localFolder.CreateFileAsync("GirlsTimes.xml", CreationCollisionOption.ReplaceExisting);
                    using (IRandomAccessStream textStream = await textFile.OpenAsync(FileAccessMode.ReadWrite))
                    {
                        using (DataWriter textWriter = new DataWriter(textStream))
                        {
                            textWriter.WriteString(loadedData.ToString());
                            await textWriter.StoreAsync();
                        }
                    }

然后我可以 write/read/update 我的 Xml。谢谢您的帮助。我验证了你的答案 Nox Noctis :)