Can I change the indenting style used when saving a DataSet to xml
I'd like to use tabs, or at least more than 2 spac开发者_开发知识库es 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)
.
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);
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);
}
精彩评论