Eric Bergman-Terrell's Blog

.NET Programming Tip: How to Format XML Attractively
October 4, 2010

The following PrettyPrintXML method formats arbitrary XML text with indentation and line breaks in the correct places. If the XML is not well-formed, an exception will be thrown. It formats the XML text using the Formatting.Indented mode of the XmlTextWriter:

private static string PrettyPrintXML(string xmlText)
{
  string prettyPrintedXML = null;

  using (MemoryStream memoryStream = new MemoryStream())
  using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.Unicode))
  {
    XmlDocument xmlDocument = new XmlDocument();
    
    // Load the XmlDocument with the XML.
    xmlDocument.LoadXml(xmlText.Trim());
  
    xmlTextWriter.Formatting = Formatting.Indented;
  
    // Write the XML into a formatting XmlTextWriter
    xmlDocument.WriteContentTo(xmlTextWriter);
    xmlTextWriter.Flush();
    memoryStream.Flush();
  
    // Have to rewind the MemoryStream in order to read
    // its contents.
    memoryStream.Position = 0;
  
    // Read MemoryStream contents into a StreamReader.
    using (StreamReader streamReader = new StreamReader(memoryStream))
    {
      // Extract the text from the StreamReader.
      prettyPrintedXML = streamReader.ReadToEnd();
    }
  }
  
  return prettyPrintedXML;
}

I use it that method in my C# Programmable Calculator app to pretty-print XML text from the clipboard:

private static String GetClipboardText()
{
  string result = "";

  IDataObject iData = Clipboard.GetDataObject();
 
  if (iData.GetDataPresent(DataFormats.Text)) 
  {
    result = (String) iData.GetData(DataFormats.Text); 
  }

  return result;
}

[Button("Debugging", "Pretty-Print XML in Clipboard")]
public static void PrettyPrintXML()
{
  string originalXML = GetClipboardText().Trim();

  string prettyPrintedXML = null;

  int startIndex = originalXML.IndexOf("<");
  int stopIndex  = originalXML.LastIndexOf("<");

  if (startIndex >= 0 && stopIndex > 0)
  {
    originalXML = originalXML.Substring(startIndex, stopIndex - startIndex + 1);

    try
    {
      prettyPrintedXML = PrettyPrintXML(originalXML);
    }
    catch (Exception)
    {
    }
  
    TextLines textLines = new TextLines();
    textLines.AddLine(prettyPrintedXML);
  
    SetClipboardText(prettyPrintedXML);
  
    iGUIUtils.DisplayTextLines("XML", textLines.Lines);
  }
  else
  {
    MessageBox.Show(originalXML, "Not Well-Formed XML", MessageBoxButtons.OK, MessageBoxIcon.Error);
  }
}
Keywords: XML, Pretty-Print, using System.Xml, C# Programmable Calculator, Indent, XmlTextWriter, Formatting.Indented

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
How to decompile Java code with JetBrains IntelliJ IDEA (2018.2.3, Windows 10)October 5, 2018
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015