Search code examples
c#xmldocument

Converting Xml To String.New Line per tag?


i am creating XmlDocument by code and adding some tag. The structure is like this

<root>
 <test>
  <test1>a</test1>
 </test>
 <test>
 </test>
  <test>
  <test1b</test1>
 </test>
</root> 

I convert the xml to string using this code

using ( var stringWriter = new StringWriter() )
using ( var xmlTextWriter = XmlWriter.Create( stringWriter ) )
    {
        xmlDoc.WriteTo( xmlTextWriter );
        xmlTextWriter.Flush( );
        return stringWriter.GetStringBuilder( ).ToString( );
    }

But when i inspect the generated string all the tags are on the same line. Is there a way to tell it to export only one tag per line


Solution

  • XmlTextWriter xmlTextWriter = new XmlTextWriter("file.xml",null);    
    xmlTextWriter.Formatting = Formatting.Indented;