2014-11-16 30 views
0

我有下面的代碼來編寫XML.But它成功運行後存儲在Bin文件夾內。 問題是:我想將它存儲在某個指定的可配置網絡位置而不是Bin文件夾中。 有什麼想法?如何在C#中的配置位置編寫和保存XMl文件

XmlDocument doc = new XmlDocument(); 
doc.LoadXml("<item><name>wrench</name></item>"); 

// Add a price element. 
XmlElement newElem = doc.CreateElement("price"); 
newElem.InnerText = "10.95"; 
doc.DocumentElement.AppendChild(newElem); 

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Indent = true; 
// Save the document to a file and auto-indent the output. 
XmlWriter writer = XmlWriter.Create("data.xml", settings); 
doc.Save(writer); 

感謝

回答

0

你只是簡單地傳遞一個路徑了,而不是data.xml中

XmlWriter.Create("C:\MyFolder\data.xml"); 

既然你想它是可配置的,我建議你使用App.Config中和ConfigurationManager來設置你想要的路徑。

基本上它會看起來像這樣

(在你的代碼:)

string path = ConfigurationManager.AppSettings["SaveFilePath"]; 

(App.Config中:)

<configuration> 
    <appSettings> 
    <add key="SaveFilePath" value="C:\BlaBla\data.xml"/> 
     </appSettings> 
</configuration> 
相關問題