2017-04-11 45 views
1

我一直在處理將配置加載和寫入XML文件的應用程序。我知道這樣做有一些意見,但我一直在這個代碼有問題。保存XML文件似乎只能保存一個條目

 private static void AddToXmlTemplate(Template tmp, string _config) 
    { 
     string configFile = _config + "configuredTemplate.xml"; 
     FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate); 
     if (File.Exists(configFile)) { 
      XDocument xD = new XDocument(); 
      xD.Add(new XElement("Store", 
       new XElement("template", 
       new XElement("filePath", tmp.TempPath), 
       new XElement("Name", tmp.TempName), 
       new XElement("description", tmp.TempDesc)))); 
      xD.Save(fs); 
      fs.Flush(); 
      fs.Dispose(); 
      //commenting for change to allow sync. 
     } 
     else 
     { 
      /********------ appends the template to the config file.------*************/ 
      XDocument xD = XDocument.Load(fs); 
      XElement root = xD.Element("Store"); 
      IEnumerable<XElement> rows = root.Descendants("template"); 
      XElement last = rows.Last(); 
      last.AddAfterSelf(
       new XElement("template"), 
       new XElement("filePath", tmp.TempPath), 
       new XElement("Name", tmp.TempName), 
       new XElement("description", tmp.TempDesc)); 
      xD.Save(fs); 
      fs.Flush(); 
      fs.Dispose(); 

     } 
    } 

這整個函數被調用另一個函數foreach循環,並且所有的功能應是檢查,看看是否有文件夾中的配置文件,檢查HTML文件,要求用戶提供信息關於這些文件,然後保存到XML文件中。

我在想,我需要將文件流操作,也可能是XDocument移動到調用函數,並將它們傳遞給這個。

最大的問題是它只保存最後一組節點。

回答

0

我認爲問題出在if (!File.Exists(configFile))。你可以試試這個:

private static void AddToXmlTemplate(Template tmp, string _config) 
    { 
     string configFile = Path.Combine(_config, "configuredTemplate.xml"); 
     using (FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate)) 
     { 
      if (!File.Exists(configFile)) 
      { 
       XElement xD = new XElement("Store", 
        new XElement("template"), 
        new XElement("filePath", tmp.TempPath), 
        new XElement("Name", tmp.TempName), 
        new XElement("description", tmp.TempDesc)); 
       xD.Save(fs); 
       fs.Flush(); 
      } 
      else 
      { 
       XDocument xD = XDocument.Load(fs); 
       XElement root = xD.Element("Store"); 
       IEnumerable<XElement> rows = root.Descendants("template"); 
       XElement last = rows.Last(); 
       last.AddAfterSelf(
        new XElement("template"), 
        new XElement("filePath", tmp.TempPath), 
        new XElement("Name", tmp.TempName), 
        new XElement("description", tmp.TempDesc)); 
       xD.Save(fs); 
       fs.Flush(); 
      } 
     } 
    } 
+0

我可以看到這應該如何工作,但是如果我們進行這些更改,它只會運行else塊。 –

+0

@ChrisRutherford可能與文件路徑有關。嘗試使用字符串configFile = Path.Combine(_config,「configuredTemplate.xml」) – daniell89

0

你的if語句的邏輯肯定是錯誤的?

當前,如果文件存在,那麼你正在創建一個新的XML文件,如果沒有,你需要添加它,它需要是另一種方式。

如果改成這樣它應該工作

if (!File.Exists(configFile)) { 
+0

必須存在一些其他奇怪的邏輯問題,因爲我原來是這樣的,但它只運行第二個塊。 –

+0

啊我認爲它是因爲你正在創建文件流,然後檢查它是否存在。請看我的其他答案。 – toby

0

啊我想它的,因爲你是第一個創建的文件流,然後檢查是否存在這樣它就會一直存在。如果您將其更改爲

private static void AddToXmlTemplate(Template tmp, string _config) 
{ 
    string configFile = _config + "configuredTemplate.xml"; 

    if (!File.Exists(configFile)) { 
     FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate) 
     XDocument xD = new XDocument(); 
     xD.Add(new XElement("Store", 
      new XElement("template", 
      new XElement("filePath", tmp.TempPath), 
      new XElement("Name", tmp.TempName), 
      new XElement("description", tmp.TempDesc)))); 
     xD.Save(fs); 
     fs.Flush(); 
     fs.Dispose(); 
     //commenting for change to allow sync. 
    } 
    else 
    { 
     FileStream fs = new FileStream(configFile, FileMode.Open);    
/********------ appends the template to the config file.------*************/ 
     XDocument xD = XDocument.Load(fs); 
     XElement root = xD.Element("Store"); 
     IEnumerable<XElement> rows = root.Descendants("template"); 
     XElement last = rows.Last(); 
     last.AddAfterSelf(
      new XElement("template"), 
      new XElement("filePath", tmp.TempPath), 
      new XElement("Name", tmp.TempName), 
      new XElement("description", tmp.TempDesc)); 
     xD.Save(fs); 
     fs.Flush(); 
     fs.Dispose(); 

    } 
} 
+0

這樣做更有意義,但是您能否確定fs和xD變量是否已妥善保存/處理以便下次運行? –

+0

您正在處理正確的FileStream。另一種方法是使用「using」(http://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it) – toby

+0

謝謝!現在我只需要解決更新塊添加新的XML聲明項的問題。 –