1

Simple question, how do i overwrite with a xmltextwriter?

I use var writer = new XmlTextWriter(exam.Path, null);. SO if i have a new document it writes as i should be.

But if the document already exists, how do i overwrite this document by a new document?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Ruben
  • 1,033
  • 3
  • 12
  • 22

2 Answers2

4

The documentation states that your usage will truncate the file and overwrite it with new content.

filename Type: System.String The filename to write to. If the file exists, it truncates it and overwrites it with the new content.

David Ruttka
  • 14,269
  • 2
  • 44
  • 39
  • Yes, I think with most .NET "writers" (i.e. streamwriter, etc) if you specify a file that already exists it will be overwritten automatically. Some have an "append" boolean parameter though I believe. – lhan Apr 26 '11 at 13:01
0

If you want to overwrite the file and find your current method appends instead you could try and delete the file in question beforehand.

e.g.

System.IO.File.Delete(exam.Path);
var writer = new XmlTextWriter(exam.Path, null);
Mike
  • 661
  • 5
  • 10