tags:

views:

56

answers:

2

I'd like to use tabs, or at least more than 2 spaces per indent level. IIRC there are options available to adjust this when using serialization to write a class out; but I don't see any way to adjust the behavior when calling MyDataSet.WriteXml(filename).

+2  A: 

You need to use a XmlTextWriter if you want to influence the layout of your XML being saved:

XmlTextWriter xtw = new XmlTextWriter(filename, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 4;
xtw.IndentChar = '\t';

and then write out your data set using that XmlTextWriter:

MyDataSet.WriteXml(xtw);
marc_s
Wouldn't that produce an indentation of 4 tab characters? ;)
Robert Jeppesen
yes, in that case, it would. Adjust as needed :-)
marc_s
+1  A: 

Use one of the overloads that accepts an XmlWriter, and pass in an XmlWriter configured with an XmlWriterSettings object that has the options you want.


XmlWriterSettings settings = new XmlWriterSettings
                             {
                                 Indent = true, 
                                 IndentChars = "\t"
                             };
using (var writer = XmlWriter.Create("file.xml", settings))
{
    ds.WriteXml(writer);
}
John Saunders