Escapes the specified text (or string version of an object) and returns a string safe to embed with an Xml document. - CSharp System.Xml

CSharp examples for System.Xml:XML Document

Description

Escapes the specified text (or string version of an object) and returns a string safe to embed with an Xml document.

Demo Code


using System.Xml.Xsl;
using System.Xml;
using System.Text.RegularExpressions;
using System.Text;
using System;// w w  w . j  av a  2 s.  c  o m

public class Main{
        #region Escape(); Unescape();
      /// <summary>
      /// Escapes the specified text (or string version of an object) and returns a string safe to embed with an Xml document.
      /// </summary>
      /// <param name="Text">The text to be escaped</param>
      /// <returns>The escaped string version of the text</returns>
      public static string Escape(object Text)
      {
         string sText = Parser.ToString(Text);
         if(sText != null && sText.Length > 0)
         {
            //escape: & (ampersand)
            sText = sText.Replace("&", "&amp;");
            //escape: <> (less than/greater than)
            sText = sText.Replace("<", "&lt;").Replace(">", "&gt;");
            //escape: "' (quote/apostrophe)
            sText = sText.Replace("\"", "&quot;").Replace("'", "&apos;");
         }
         return sText;
      }
}

Related Tutorials