2016-04-14 96 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

相關問題