Indentation and new line command for XMLwriter in C#

前端 未结 6 809
-上瘾入骨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:32

    Use Settings As follow:

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Encoding = Encoding.UTF8;
            settings.Indent = true;
    
            using (MemoryStream memoryStream = new MemoryStream())
            using (XmlWriter xmlDoc = XmlWriter.Create(memoryStream, settings)){
             // logic here..
            }
    

    This will get you where you want, though, you don't have to use MemoryStream, the importent part is the settings.

提交回复
热议问题