【问题标题】:Can I change the indenting style used when saving a DataSet to xml我可以更改将 DataSet 保存到 xml 时使用的缩进样式吗
【发布时间】:2009-12-12 06:11:19
【问题描述】:

我想使用制表符,或者每个缩进级别至少有 2 个以上的空格。 IIRC 在使用序列化写出一个类时,有一些选项可以调整它;但在调用MyDataSet.WriteXml(filename) 时,我看不到任何调整行为的方法。

【问题讨论】:

    标签: c# xml dataset


    【解决方案1】:

    如果您想影响正在保存的 XML 的布局,您需要使用 XmlTextWriter

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

    然后使用XmlTextWriter写出你的数据集:

    MyDataSet.WriteXml(xtw);
    

    【讨论】:

    • 不会产生 4 个制表符的缩进吗? ;)
    【解决方案2】:

    使用接受XmlWriter 的重载之一,并传入配置有XmlWriterSettings 对象的XmlWriter,该对象具有您想要的选项。


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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多