Get Formatted Xml : Xml Format « 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.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » XML » Xml FormatScreenshots 
Get Formatted Xml
 

using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Text;

namespace LateBindingApi.CodeGenerator.ComponentAnalyzer
{
    /// <summary>
    /// offers various helper functions
    /// </summary>
    internal static class Utils
    {   
        #region Ressource Methods

        /// <summary>
        /// Read RessourceText
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        internal static string ReadTextFileFromRessource(string ressourcePath)
        {
            System.IO.Stream ressourceStream = null;
            System.IO.StreamReader textStreamReader = null;
            try
            {
                string assemblyName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
                ressourcePath = assemblyName + "." + ressourcePath;
                ressourceStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourcePath);
                if (ressourceStream == null)
                    throw (new System.IO.IOException("Error accessing resource Stream."));

                textStreamReader = new System.IO.StreamReader(ressourceStream);
                if (textStreamReader == null)
                    throw (new System.IO.IOException("Error accessing resource File."));

                string text = textStreamReader.ReadToEnd();
                return text;
            }
            catch (Exception exception)
            {
                throw (exception);
            }
            finally
            {
                if(null!=textStreamReader)
                    textStreamReader.Close();
                if (null != ressourceStream)
                    ressourceStream.Close();
            }
        }
        
        #endregion

        #region Xml Methods

        /// <summary>
        /// Gets friendly inner xml from node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public static String GetFormattedXml(XmlNode node)
        {
            string result = "";
            
            MemoryStream memStream = new MemoryStream();
            XmlTextWriter textWriter = new XmlTextWriter(memStream, Encoding.UTF8);
            textWriter.Formatting = Formatting.Indented;
            node.WriteContentTo(textWriter);
            textWriter.Flush();
            memStream.Flush();
            memStream.Position = 0;

            StreamReader sReader = new StreamReader(memStream);   
            String FormattedXML = sReader.ReadToEnd();
            result = FormattedXML;
            memStream.Close();
            textWriter.Close();

            return result;
        }

        #endregion

        #region Guid Methods

        /// <summary>
        /// returns a new xml encoded guid as string
        /// </summary>
        /// <returns></returns>
        public static string NewEncodedGuid()
        {
            return XmlConvert.EncodeName(Guid.NewGuid().ToString());
        }

        /// <summary>
        /// xml encode guid and remove the chars"{}" if exists
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static string EncodeGuid(string guid)
        {
            return XmlConvert.EncodeName(guid.Replace("{""").Replace("}"""));
        }

        #endregion

        #region IO Methods

        /// <summary>
        /// Removes bad chars from filePath
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string RemoveBadChars(string filePath)
        {
            if (filePath == null)
                return "";

            string validateFilePath = filePath.Replace("\0""");

            char[] badChars = System.IO.Path.GetInvalidPathChars();
            char replaceChar  = new char();
            
            foreach (char item in badChars)
                validateFilePath = validateFilePath.Replace(item, replaceChar);

            return validateFilePath;
        }

        #endregion
    }
}

   
  
Related examples in the same category
1.Format Xml
2.Clean Xml
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.