Indentation and new line command for XMLwriter in C#

前端 未结 6 797
-上瘾入骨i
-上瘾入骨i 2020-12-09 08:10

I am writing some data to XML file...but when I open it all the values are in a single line...how can write it in readable format?ie each node in new line and indentation?

6条回答
  •  既然无缘
    2020-12-09 08:36

    A little VB.NET version using XmlWriter plus XmlWriterSettings direct to file:

    Dim oSerializer As New XmlSerializer(GetType(MyType), New XmlRootAttribute("MyRootAttributeName"))
    Using oXmlWriter As XmlWriter = XmlWriter.Create("MyFilePath", New XmlWriterSettings With {.Indent = True}) //Default encoding is utf-8
        oSerializer.Serialize(oXmlWriter, oInstanceOfMyType)
        oXmlWriter.Flush()
    End Using
    

    Check the doco for other settings and file overwrite options but this is clean and works as expected in lots of situations.

提交回复
热议问题