【发布时间】:2009-12-12 06:11:19
【问题描述】:
我想使用制表符,或者每个缩进级别至少有 2 个以上的空格。 IIRC 在使用序列化写出一个类时,有一些选项可以调整它;但在调用MyDataSet.WriteXml(filename) 时,我看不到任何调整行为的方法。
【问题讨论】:
我想使用制表符,或者每个缩进级别至少有 2 个以上的空格。 IIRC 在使用序列化写出一个类时,有一些选项可以调整它;但在调用MyDataSet.WriteXml(filename) 时,我看不到任何调整行为的方法。
【问题讨论】:
如果您想影响正在保存的 XML 的布局,您需要使用 XmlTextWriter:
XmlTextWriter xtw = new XmlTextWriter(filename, Encoding.UTF8);
xtw.Formatting = Formatting.Indented;
xtw.Indentation = 4;
xtw.IndentChar = '\t';
然后使用XmlTextWriter写出你的数据集:
MyDataSet.WriteXml(xtw);
【讨论】:
使用接受XmlWriter 的重载之一,并传入配置有XmlWriterSettings 对象的XmlWriter,该对象具有您想要的选项。
XmlWriterSettings settings = new XmlWriterSettings
{
Indent = true,
IndentChars = "\t"
};
using (var writer = XmlWriter.Create("file.xml", settings))
{
ds.WriteXml(writer);
}
【讨论】: