title: .NET 6 XmlSerializer Pretty print
body: <p>I've this sample .NET 6 program printing out a serialised object to XML:</p>
  <pre><code>using System.Text;
  using System.Xml.Serialization;
  
  var serializer = new XmlSerializer(typeof(Order));
  
  var order = new Order
  {
      Address = new Address
      {
          FirstName = &quot;Name&quot;
      }
  };
  
  await using var memoryStream = new MemoryStream();
  var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8);
  serializer.Serialize(streamWriter, order);
  
  var result = Encoding.UTF8.GetString(memoryStream.ToArray());
  
  Console.WriteLine(result);
  
  public class Order
  {
      public Address Address;
  }
  
  public class Address
  {
      public string FirstName;
  }
  </code></pre>
  <p>This results in this output:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;Order xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;&lt;Address&gt;&lt;FirstName&gt;Name&lt;/FirstName&gt;&lt;/Address&gt;&lt;/Order&gt;
  </code></pre>
  <p>In .NET 5 and .NET Core 3 similar code results in pretty printed XML like below. How can I format this XML in .NET6?</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;Order xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;&gt;
    &lt;Address&gt;
      &lt;FirstName&gt;Name&lt;/FirstName&gt;
    &lt;/Address&gt;
  &lt;/Order&gt;
  </code></pre>