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

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » XML TreeScreenshots 
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.