Formatting of XML created by DataContractSerializer

前端 未结 5 1489
不思量自难忘°
不思量自难忘° 2021-02-01 00:35

Is there an easy way to get DataContractSerializer to spit out formatted XML rather then one long string? I don\'t want to change the tags or content in any way, just have it ad

5条回答
  •  难免孤独
    2021-02-01 01:10

    As bendewey says, XmlWriterSettings is what you need - e.g. something like

    var ds = new DataContractSerializer(typeof(Foo));
    
    var settings = new XmlWriterSettings { Indent = true };
    
    using (var w = XmlWriter.Create("fooOutput.xml", settings))
        ds.WriteObject(w, someFoos);
    

提交回复
热议问题