Prevent XmlSerializer from formatting output

前端 未结 3 1862
长情又很酷
长情又很酷 2020-12-20 11:26

When using the default settings with the XmlSerializer it will output the XML as a formated value.

IE: something along these lines.



        
3条回答
  •  悲&欢浪女
    2020-12-20 11:56

    Not very intuitive, but the Indent property on the XmlWriterSettings controls the whole formatting:

    var serializer = new XmlSerializer(typeof(MyClass));
    
    using (var writer = new StreamWriter("file.path"))
    using (var xmlWriter = XmlWriter.Create(writer, new XmlWriterSettings { Indent = false }))
    {
        serializer.Serialize(xmlWriter, myObject);
    }
    

    There are a few more options on XmlWriterSettings that you might want to explore.

提交回复
热议问题