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();
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.