/// <summary>
 /// Creates a Simple XML parser object.
 /// Call Go(BufferedReader) immediately after creation.
 /// </summary>
 private SimpleXmlParser(ISimpleXmlDocHandler doc, ISimpleXmlDocHandlerComment comment, bool html)
 {
     Doc          = doc;
     this.comment = comment;
     Html         = html;
     Stack        = new Stack();
     State        = html ? Text : Unknown;
 }
        /// <summary>
        /// Parses the XML document firing the events to the handler.
        /// @throws IOException on error
        /// </summary>
        /// <param name="doc">the document handler</param>
        /// <param name="inp">the document. The encoding is deduced from the stream. The stream is not closed</param>
        public static void Parse(ISimpleXmlDocHandler doc, Stream inp)
        {
            var b4    = new byte[4];
            var count = inp.Read(b4, 0, b4.Length);

            if (count != 4)
            {
                throw new IOException("Insufficient length.");
            }

            var    encoding = getEncodingName(b4);
            string decl     = null;

            if (encoding.Equals("UTF-8"))
            {
                var sb = new StringBuilder();
                int c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == '>')
                    {
                        break;
                    }

                    sb.Append((char)c);
                }
                decl = sb.ToString();
            }
            else if (encoding.Equals("CP037"))
            {
                var bi = new MemoryStream();
                int c;
                while ((c = inp.ReadByte()) != -1)
                {
                    if (c == 0x6e) // that's '>' in ebcdic
                    {
                        break;
                    }

                    bi.WriteByte((byte)c);
                }
                decl = EncodingsRegistry.Instance.GetEncoding(37).GetString(bi.ToArray());//cp037 ebcdic
            }
            if (decl != null)
            {
                decl = getDeclaredEncoding(decl);
                if (decl != null)
                {
                    encoding = decl;
                }
            }
            Parse(doc, new StreamReader(inp, IanaEncodings.GetEncodingEncoding(encoding)));
        }
 public static void Parse(ISimpleXmlDocHandler doc, TextReader r)
 {
     Parse(doc, null, r, false);
 }
        /// <summary>
        /// Parses the XML document firing the events to the handler.
        /// @throws IOException on error
        /// </summary>
        /// <param name="doc">the document handler</param>
        /// <param name="comment"></param>
        /// <param name="r">the document. The encoding is already resolved. The reader is not closed</param>
        /// <param name="html"></param>
        public static void Parse(ISimpleXmlDocHandler doc, ISimpleXmlDocHandlerComment comment, TextReader r, bool html)
        {
            var parser = new SimpleXmlParser(doc, comment, html);

            parser.go(r);
        }