Pretty Print XML : XML Tree « XML « C# / C Sharp






Pretty Print XML

 
#region License and Copyright
/*
 * Dotnet Commons Xml
 *
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
 * for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this library; if not, write to the 
 * Free Software Foundation, Inc., 
 * 59 Temple Place, 
 * Suite 330, 
 * Boston, 
 * MA 02111-1307 
 * USA 
 * 
 */
#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.Serialization;

//using Dotnet.Commons.Reflection;

namespace Dotnet.Commons.Xml
{
  
  ///  
  /// <summary>
  /// This utility class contains wrapper functions that help to ease the handling and 
    /// manipulation of Xml documents, such as adding an element, adding an attribute
    /// to an element, copying and cloning of nodes, etc.
  ///
  /// </summary>
  /// 
    
  public abstract class XmlUtils
  {
    /// -----------------------------------------------------------
    /// <summary>
    /// Pretty Print XML
    /// </summary>
    /// <param name="xml"></param>
    /// <returns>Pretty print xml string</returns>
    /// <remarks> Thanks to http://dotnet.editme.com/codePrettyPrintXML</remarks>
    /// -----------------------------------------------------------
    public static string PrettyPrint(string xml)
    {
      XmlDocument xmlDom   = new XmlDocument();

      // Load the XmlDocument with the XML.
      xmlDom.LoadXml(xml);

      return PrettyPrint(xmlDom);
    }


    /// -----------------------------------------------------------
    /// <summary>
        /// Print Print an <see cref="XmlDocument"/>
    /// </summary>
    /// <param name="xmlDom"></param>
    /// <returns></returns>
    /// -----------------------------------------------------------
    public static string PrettyPrint(XmlDocument xmlDom)
    {
            return PrettyPrint(xmlDom, true);
    }


        /// <summary>
        /// Print Print an <see cref="XmlNode"/>
        /// </summary>
        /// <param name="xmlNode"></param>
        /// <returns></returns>
        public static string PrettyPrint(XmlNode xmlNode)
        {
            return PrettyPrint(xmlNode, false);
        }


        /// <summary>
        /// Pretty Print an <see cref="XmlDocument"/> or an <see cref="XmlNode"/>
        /// </summary>
        /// <param name="xmlObj"></param>
        /// <param name="isXmlDoc"></param>
        /// <returns></returns>
        public static string PrettyPrint(object xmlObj, bool isXmlDoc)
        {

            if (!(xmlObj is XmlDocument || xmlObj is XmlDataDocument ||
                xmlObj is XmlNode || xmlObj is XmlElement))
                throw new ArgumentException("xmlObj must be either an XmlDocument, XmlDataDocument, XmlNode or an XmlElement object");

            String prettyXml = "";

            MemoryStream memStream = new MemoryStream();
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memStream, Encoding.Unicode);

            try
            {
                xmlTextWriter.Formatting = Formatting.Indented;
                xmlTextWriter.Indentation = 4;
                xmlTextWriter.QuoteChar = '\'';


                // Write the XML into a formatting XmlTextWriter
                if (xmlObj is XmlDocument)
                    ((XmlDocument)xmlObj).WriteContentTo(xmlTextWriter);
                else if (xmlObj is XmlDataDocument)
                    ((XmlDataDocument)xmlObj).WriteContentTo(xmlTextWriter);
                else
                    ((XmlNode)xmlObj).ParentNode.WriteContentTo(xmlTextWriter);

                
                xmlTextWriter.Flush();
                memStream.Flush();

                // Have to rewind the MemoryStream in order to read
                // its contents.
                memStream.Position = 0;

                // Read MemoryStream contents into a StreamReader.
                StreamReader streamReader = new StreamReader(memStream);

                // Extract the text from the StreamReader.
                String sFormattedXML = streamReader.ReadToEnd();

                prettyXml = sFormattedXML;
            }
            catch
            {
            }

            memStream.Close();
            xmlTextWriter.Close();

            return prettyXml;
        }
    }
}

   
  








Related examples in the same category

1.Display XML file content to TreeViewDisplay XML file content to TreeView
2.Read an XML Document and display the file as a TreeRead an XML Document and display the file as a Tree