[英]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.