2016-04-14 102 views
1

我有这个XML:保存与C#中的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> 
+0

确保项目至少有一行。 – jdweng

+0

在调用Save之前,打印'loadedData.ToString()'。你在那看到什么? –

+0

@Nox Noctis,我做了一个编辑。 xml更新的很好... –

回答

1

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

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

+0

我会试试这个;)谢谢 –

1

Wouaouhhh,经过多次searchs和尝试,我已经解决:

这是我读的方法(我知道是不是使用try正道/捕获/终于...)):

 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 :)

+0

我不知道win 10的uwp呢,但是在8.1的商店项目中没有其他方法来检查文件是否存在,除了尝试打开它并捕获FileNotFountException。 –

+0

我发现没有更好的作品,但对于W10 ...也许与更新会有更好的:) –

+0

System.IO.File.Exists(路径)应该工作在uwp。 例如:if(System.IO.File.Exists(Path.Combine(ApplicationData.Current.LocalFolder,GirlsTimes.xml)) { // TODO:该文件存在 } – Akos

相关问题