Le code ci-dessous crée un flux XML non indenté et utilise les objets XmlReader et XmlWriter pour le lire et le reformater de façon à l'indenter. Tout se trouve dans la méthode GetIndentedXml.

Il s'agit d'un programme Console, donc vous pouvez le tester tel quel en le collant dans un fichier Program.cs d'un projet Console.

using System;
using System.Text;
using System.Xml;
using System.IO;

namespace ConsoleXmlDemo
{
    class Program
    {
        /// <summary>
        /// Retourne un flux XML sans indentation
        /// </summary>
        /// <returns></returns>
        static MemoryStream GetRawXml()
        {
            return new MemoryStream(ASCIIEncoding.UTF8.GetBytes(@"<Root>
    <Example id=""1"" >
<Sub>Text</Sub>
</Example>
<Example       id=""2""><Sub>Text 2</Sub></Example></Root>
"),false);
        }

        /// <summary>
        /// Retourne un flux XML avec indentation
        /// </summary>
        /// <param name="inputXml">Flux de XML brut pouvant être lu</param>
        /// <returns></returns>
        static MemoryStream GetIndentedXml(Stream inputXml)
        {
            if (inputXml == null)
                throw new ArgumentNullException("inputXml");
            if (!inputXml.CanRead)
                throw new ArgumentException("Le flux ne peut être lu.");

            MemoryStream ms = new MemoryStream();

            XmlWriter writer = XmlWriter.Create(
            ms,
            new XmlWriterSettings()
            {
                Indent = true,
                IndentChars = "\t",
            }
                );

            using (XmlReader reader = XmlReader.Create(
                inputXml,
                new XmlReaderSettings()
                {
                    CloseInput = true,
                    IgnoreWhitespace = true
                }
                ))
            {
                writer.WriteNode(reader, true);
            }

            writer.Close();
            return ms;
            
        }

        static void Main(string[] args)
        {
            Console.WriteLine("Xml brut:");
            Console.WriteLine(ASCIIEncoding.UTF8.GetString(GetRawXml().ToArray()));

            Console.WriteLine("Xml formaté:");
            Console.WriteLine(ASCIIEncoding.UTF8.GetString(GetIndentedXml(GetRawXml()).ToArray()));
            
            Console.WriteLine("Appuyez sur une touche pour terminer...");
            Console.ReadKey();
        }
    }
}

L'important pour que cela fonctionne est à la ligne 55:

IgnoreWhitespace = true

En effet, le formatage ne fonctionne pas si le code XML lu contient des caractères blancs.

La sortie produite est la suivante :

Xml brut:
<Root>
    <Example id="1" >
<Sub>Text</Sub>
</Example>
<Example       id="2"><Sub>Text 2</Sub></Example></Root>

Xml formaté:
?<?xml version="1.0" encoding="utf-8"?>
<Root>
        <Example id="1">
                <Sub>Text</Sub>
        </Example>
        <Example id="2">
                <Sub>Text 2</Sub>
        </Example>
</Root>
Appuyez sur une touche pour terminer...