0

I am trying to create an XML file for storing some info from a class. I gave the path as a const set to Info.xml. but the XMLTextWriter is not saving the XML file to bin(debug/release) folder, rather it is saving it to MyDocuments folder. When i tried to check if the file exists with File.Exists(path), its returning false. Could somebody please guide me here.

My code looks as below...

string BookName = "Book1" 
string AuthorName= "Author1" 
XmlTextWriter writer = new XmlTextWriter("Info.xml", Encoding.UTF8);    writer.WriteStartDocument(); 
writer.WriteStartElement("Title"); 
writer.WriteString(BookName); 
writer.WriteStartElement("Author"); 
writer.WriteString(AuthorName); 
writer.WriteEndElement(); 
writer.WriteEndElement(); 
writer.WriteEndDocument(); 
writer.Close();
Developer
  • 309
  • 5
  • 8
  • 19

1 Answers1

0

Try this

    public void SerializeToXML(YourClass yourObj)
    {
        if (!File.Exists("C:\\Info.xml"))
            File.Create("C:\\Info.xml");
        XmlSerializer serializer = new XmlSerializer(typeof(YourClass));
        System.IO.TextWriter textWriter = new StreamWriter("C:\\Info.xml");
        serializer.Serialize(textWriter, yourObj);
        textWriter.Close();
    }

This would save "Info.xml" to your C:, neither in debug nor in release. (It would also create file if doesn't already exists).

Check out this link as well, it might help.

Devraj Gadhavi
  • 3,541
  • 3
  • 38
  • 67
  • Hi all, Thanks for the quick response, – Developer Nov 07 '12 at 08:55
  • My code looks as below...string BookName = "Book1" string AuthorName= "Author1" XmlTextWriter writer = new XmlTextWriter("Info.xml", Encoding.UTF8); writer.WriteStartDocument(); writer.WriteStartElement("Title"); writer.WriteString(BookName); writer.WriteStartElement("Author"); writer.WriteString(AuthorName); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Close(); – Developer Nov 07 '12 at 08:56
  • 1
    Forgot to mention, I am developing a COM addIn and Application.startupPath gives me the path "C:\\Program Files (x86)\\Microsoft Office\\Office14" – Developer Nov 07 '12 at 08:59
  • @Sri how are you saving the file? Could you edit your question & post your code? If you are not giving anything except the file name as path, any com application is going to save it in it's default location. – Devraj Gadhavi Nov 07 '12 at 09:30
  • Hi Devraj, I have updated my question above with the code i am using. – Developer Nov 07 '12 at 09:39
  • when i use "AppDomain.CurrentDomain.BaseDirectory.ToString()", I am getting the path of bin directory. I hope this solves my problem. Thanks for your help.... – Developer Nov 07 '12 at 09:41
  • I would suggest you save your file in folder returned by -Environment.GetFolderPath(SpecialFolder.ApplicationData). As there might be some permission issues in future. Also you can save your file wherever you want by specifying the full path in - XmlTextWriter writer = new XmlTextWriter("C:\\Info.xml", Encoding.UTF8); See my edited answer. – Devraj Gadhavi Nov 07 '12 at 09:56
  • Environment.GetFolderPath(SpecialFolder.ApplicationData) works for me. Thanks so much for the help Devraj. – Developer Nov 07 '12 at 10:52