c# DataSet.WriteXml(fileStream, XmlWriteMode.IgnoreSchema); corrupts my xml file

<Data>
  <configs>
    <someEntry>"value1"</someEntry>
  </configs>
  <profiles>
    <someEntry>"value2"</someEntry>
  </profiles>
</Data>es>
    <someEntry>"value3"</someEntry>
  </profiles>
</Data>

That </Data>es> is the problem. It happens when I try to delete the "value3" DataRow in this example. The DataTable Visualizer of Visual Studio shows that my table is fine but the DataSet gets saved that way although it's suppossed to be this way:

<Data>
  <configs>
    <someEntry>"value1"</someEntry>
  </configs>
  <profiles>
    <someEntry>"value2"</someEntry>
  </profiles>
</Data>

Before starting using FileStream everything worked as expected. This is how I initiate my FileStream:

FileStream filestream = new FileStream(xmlLocation, FileMode.Open, FileAccess.ReadWrite);
Jon Skeet
people
quotationmark

You're using FileMode.Open, which doesn't truncate the file if it already exists.

Just use File.Create instead:

using (var stream = File.Create(directory))
{
    ...
}

(You could specify FileMode.Create instead, as your post originally had before you edited it, but I find it much simpler to call the File.* simplified methods in almost all cases.)

people

See more on this question at Stackoverflow