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

时间:2016-04-14 14:38:43

标签: c# xml windows-10-universal

我有这个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>

2 个答案:

答案 0 :(得分:1)

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

有关详细信息,请参阅this article

答案 1 :(得分:1)

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();
                        }
                    }

然后我可以写/读/更新我的Xml。谢谢您的帮助。我有效你的答案Nox Noctis :)