简体   繁体   中英

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. Question is:I want to store it in some specified Configurable Network location instead of Bin folder. 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

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.

Basically it'd look like this

(In your code:)

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

(App.Config:)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM