In C# how do you write a DataSet to file without it being written with pretty print?
Using C# and .NET 2.0, I’ve been using dataSet.WriteXml(fileName, XmlWriteMode.IgnoreSchema), which by default is writing the Xml file with pretty print. The company consuming the Xml files I write suggested that writing without the pretty print will not affect them, and will significantly decrease the size of the files. With a little playing around in the System.Xml namespace, I did find a solution. However, in my searching I did not find the answer anywhere, so I thought it might be helpful to someone else in the future if I posted the question. Also, I wouldn’t be surprised at all if there’s a better or at least different way of accomplishing this.
For those that don’t know (I didn’t until today), Xml ‘pretty print’ is:
<?xml version='1.0' standalone='yes'?> <NewDataSet> <Foo> <Bar>abc</Bar> </Foo> </NewDataSet>
Without pretty print it looks like this:
<?xml version='1.0' encoding='utf-8'?><NewDataSet><Foo><Bar>abc</Bar></Foo></NewDataSet>
Additionally, the size savings was significant, 70mb files are becoming about 40mb. I’ll post my solution later today if no one else has.
It’s pretty simple, you just have to create an
XmlWriterusing anXmlWriterSettingswhich has theIndentproperty set to false: