2012-05-29 42 views
3

我想創建一個xml文件。我知道如何使用linq to xml概念創建一個xml文件。 但我想將該文件保存在我的項目的bin文件夾中。怎麼做。在c#中創建一個xml文檔並將該文件存儲到項目的bin文件夾中

XDocument changesetDB = new XDocument(
        new XElement("changes", 
          new XElement("change", 
           new XAttribute("path", changedFile), 
           new XAttribute("changesetID", changesetID), 
           new XAttribute("JIRAID", issueID)))); 

現在我想將它保存在bin文件夾中。有沒有可能這樣做。 謝謝,

+0

'XDocument.Save'? – carlosfigueira

+0

是的,我也一樣認爲..但保存意味着它將保存文件? – Searcher

+0

不要忘了標記答案如果你有你想要的信息... –

回答

6

嘗試:XmlDocument.Save Method (String)

string path = Path.GetDirectoryName(Application.ExecutablePath) ; 
changesetDB .Save(Path.Combine(path , "data.xml")); 
+0

使用'Path.Combine()'! –

+0

@JeffMercado - 更新謝謝你的建議...... –

0

你的exe將在Bin \ Debug或斌創建\ Rease所以.. \將bin文件夾。但是,看看你的目錄,當你使用你的程序了對比的

changesetDB.Save("..\changesetDB.xml"); 
3
changesetDB.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"myfile.xml")); 

這將節省myfile.xmlbin/debug文件夾

但是,如果你想文件保存到文件夾bin,不調試或發佈,那麼你必須從路徑中去除路徑的調試部分。您可以執行以下操作。

string binDir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd(@"Debug\\".ToCharArray()); 
changesetDB.Save(Path.Combine(binDir,"myfile.xml")); 

這會將文件myfile.xml保存到文件夾bin

相關問題