簡體   English   中英

如何在C#中的配置位置中寫入和保存XMl文件

[英]How to write and save an XMl file in a Configured Location in C#

我有以下代碼來編寫XML,但是成功運行后它將存儲在Bin文件夾中。 問題是:我想將其存儲在某些指定的可配置網絡位置而不是Bin文件夾中。 任何想法?

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

謝謝

您只需在此處傳遞路徑,而不是在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM