[C#] Formatierte XML-Ausgabe

/// <summary->
/// formatiert XML code
/// </summary->
/// <param name=&quot;xml&quot;->input string mit XML-code</param->
/// <param name=&quot;indentation&quot;->Anzahl Zeichen für die Einrückung</param->
/// <param name=&quot;indentchar&quot;->Einrückungszeichen</param->
/// <returns->formatierter XML-code</returns->
public string FormatXML(string xml, int indentation, char indentchar)
{
    string res = string.Empty;

    MemoryStream ms = null;
    XmlTextWriter tw = null;

    try
    {
        XmlDocument doc = new XmlDocument();

        ms = new MemoryStream();
        tw = new XmlTextWriter(ms, Encoding.Unicode);

        doc.LoadXml(xml);

        tw.Formatting = Formatting.Indented;
        tw.Indentation = indentation;
        tw.IndentChar = indentchar;

        doc.WriteContentTo(tw);

        tw.Flush();
        ms.Flush();

        ms.Position = 0;

        StreamReader sr = new StreamReader(ms);

        res = sr.ReadToEnd();
    }
    catch (XmlException)
    {
    }
    finally
    {
        ms.Close();
        tw.Close();
    }

    return res;
}