Escape XML - CSharp System.Xml

CSharp examples for System.Xml:XML String

Description

Escape XML

Demo Code

/**/*from   ww w  .j a v a  2 s  .  com*/
         * Returns the IANA encoding name that is auto-detected from
         * the bytes specified, with the endian-ness of that encoding where appropriate.
         * (method found in org.apache.xerces.impl.XMLEntityManager, originally published
         * by the Apache Software Foundation under the Apache Software License; now being
         * used in iText under the MPL)
         * @param b4    The first four bytes of the input.
         * @return an IANA-encoding string
         * @since 5.0.6
         */
using System.Text;
using System;

public class Main{
        /**
         * Escapes a string with the appropriated XML codes.
         * @param s the string to be escaped
         * @param onlyASCII codes above 127 will always be escaped with &amp;#nn; if <CODE>true</CODE>
         * @return the escaped string
         * @since 5.0.6
         */
        public static String EscapeXML(String s, bool onlyASCII) {
            char[] cc = s.ToCharArray();
            int len = cc.Length;
            StringBuilder sb = new StringBuilder();
            for (int k = 0; k < len; ++k) {
                int c = cc[k];
                switch (c) {
                    case '<':
                        sb.Append("&lt;");
                        break;
                    case '>':
                        sb.Append("&gt;");
                        break;
                    case '&':
                        sb.Append("&amp;");
                        break;
                    case '"':
                        sb.Append("&quot;");
                        break;
                    case '\'':
                        sb.Append("&apos;");
                        break;
                    default:
                        if (IsValidCharacterValue(c)) {
                            if (onlyASCII && c > 127)
                                sb.Append("&#").Append(c).Append(';');
                            else
                                sb.Append((char)c);
                        }
                        break;
                }
            }
            return sb.ToString();
        }
        /**
         * Checks if a character value should be escaped/unescaped.
         * @param   c   a character value
         * @return  true if it's OK to escape or unescape this value 
         */
        public static bool IsValidCharacterValue(int c) {
            return (c == 0x9 || c == 0xA || c == 0xD
                    || c >= 0x20 && c <= 0xD7FF
                    || c >= 0xE000 && c <= 0xFFFD
                    || c >= 0x10000 && c <= 0x10FFFF);
        }
        /**
         * Checks if a character value should be escaped/unescaped.
         * @param   s   the String representation of an integer
         * @return  true if it's OK to escape or unescape this value 
         */
        public static bool IsValidCharacterValue(String s) {
            try {
                int i = int.Parse(s);
                return IsValidCharacterValue(i);
            }
            catch {
                return false;
            }
        }
}

Related Tutorials