[英]How to write and save an XMl file in a Configured Location in C#
I have the below code to write an XML.But it stores inside the Bin folder after successful run. 我有以下代码来编写XML,但是成功运行后它将存储在Bin文件夹中。 Question is:I want to store it in some specified Configurable Network location instead of Bin folder. 问题是:我想将其存储在某些指定的可配置网络位置而不是Bin文件夹中。 Any idea? 任何想法?
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);
Thanks 谢谢
You just simply pass a path there instead of data.xml 您只需在此处传递路径,而不是在data.xml中
XmlWriter.Create("C:\MyFolder\data.xml");
Since you'd like it to be configurable, I'd suggest you use App.Config and ConfigurationManager to set the path you'd like. 由于您希望它是可配置的,因此建议您使用App.Config和ConfigurationManager设置所需的路径。
Basically it'd look like this 基本上看起来像这样
(In your code:) (在您的代码中:)
string path = ConfigurationManager.AppSettings["SaveFilePath"];
(App.Config:) (App.Config :)
<configuration>
<appSettings>
<add key="SaveFilePath" value="C:\BlaBla\data.xml"/>
</appSettings>
</configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.