Prevent XmlSerializer from formatting output

前端 未结 3 1834
长情又很酷
长情又很酷 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:45

    It is simple to parse the resulting XML and remove and newlines and tabs...
    using 'Indent = false', will still put elements on newlines no?

    0 讨论(0)
  • 2020-12-20 11:46

    ..

    XmlSerializer xmlser = new XmlSerializer(...);
    
    XmlWriterSettings settings = new XmlWriterSettings {Indent = false};
    
    using (XmlWriter xw = XmlWriter.Create(stream, settings))
    {
    

    ...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题