1

Please suggest me a method to save an XML file to the current installation directory of the application created using C#.

7
  • 8
    I would advise against saving files into the same directory as the application. Use the user's isolated storage directory instead. Commented Sep 13, 2009 at 9:39
  • can you please give an example for that Commented Sep 13, 2009 at 9:49
  • Which XML library? XDocument, XmlDocument, ...? Commented Sep 13, 2009 at 9:50
  • 1
    A broad overview of things you can do with Isolated Storage: msdn.microsoft.com/en-us/library/8dzkff1s.aspx Commented Sep 13, 2009 at 9:51
  • 1
    Introduction to Isolated Storage: msdn.microsoft.com/en-us/library/3ak841sy.aspx Commented Sep 13, 2009 at 10:03

4 Answers 4

8
  1. Create an XML file: The easiest way is to create and populate an XmlDocument or XDocument object.

  2. Save to the install directory: use

   string path = System.IO.Path.GetDirectoryName(Application.ExecutablePath);  
   string file = System.IO.Path.Combine(pathm, "myfile.xml");

But you do know that the application's folder isn't the best place to store a file, right?

Edit:

Some comments mention Isolated Storage, but that is overkill. The best way to store data is to use the appropriate DataPath. That is different under various versions of Windows, but this always works:

string path = 
    Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

There are a few other values in the Environment.SpecialFolder enum, take a look.

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for SpecialFolder.ApplicationData. Hope you don't mind my editing your answer, but I hate the horizontal scrollbar for code samples.
Yep +1 for ApplicationData. In truth I was probably thinking of that when I suggested Isolated Storage.
7

Using XDocument and LINQ:

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Directory.GetCurrentDirectory() + "/myXML.xml");

or

XDocument myXml = new XDocument(new XElement("Node 1", new XElement("Node 2")));
myXml.Save(Path.GetDirectoryName(Application.ExecutablePath) + "/myXML.xml");

1 Comment

I know the code works with /myXML.xml instead of \myXML.xml, but it sure looks wrong. I'd go with Henk's Path.Combine().
0

StringBuilders and HttpUtility.HtmlEncode usually work quite well.

Comments

0

And, after you've tried everything and are still scratching your head, wondering why it isn't working...

  1. Make sure the folder you want to save to ALLOWS Writing by the account the Web service is running under or the user (depending on how you have security configured); this should be done at the NTFS level. Previous suggestions said that it would be bad to write in the application folder. No, it wouldn't be bad, it would be incredibly stupid to write there, not just bad. So, pick somewhere else to save the data. Ideally on another physical volume, or better still: post the XML file to another application on another server who's only lot in life is saving this data.
  2. Assuming you know how to create an XML document in C# (strings ain't it - use the proper methods for building an XmlDocument - like by using elements and nodes and attributes), you can use the .Save method
  3. Why not use strings??? Because you'll spend more time debugging "well-formed XML" errors than you will solving your actual problem. People are lousy at making well-formed XML. Machines are pretty good at it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.