Пример #1
0
        public void ReadContent()
        {
            string name;

            if (IsWhitespace(Peek()))
            {
                if (buffer.Length == 0)
                {
                    isWhitespace = true;
                }
                HandleWhitespaces();
            }
            if (Peek() == '<')
            {
                Read();
                switch (Peek())
                {
                case '!':                 // declarations
                    Read();
                    if (Peek() == '[')
                    {
                        Read();
                        if (ReadName() != "CDATA")
                        {
                            throw Error("Invalid declaration markup");
                        }
                        Expect('[');
                        ReadCDATASection();
                        return;
                    }
                    else if (Peek() == '-')
                    {
                        ReadComment();
                        return;
                    }
                    else if (ReadName() != "DOCTYPE")
                    {
                        throw Error("Invalid declaration markup.");
                    }
                    else
                    {
                        throw Error("This parser does not support document type.");
                    }

                case '?':                 // PIs
                    HandleBufferedContent();
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string text = String.Empty;
                    if (Peek() != '?')
                    {
                        while (true)
                        {
                            text += ReadUntil('?', false);
                            if (Peek() == '>')
                            {
                                break;
                            }
                            text += "?";
                        }
                    }
                    handler.OnProcessingInstruction(
                        name, text);
                    Expect('>');
                    return;

                case '/':                 // end tags
                    HandleBufferedContent();
                    if (elementNames.Count == 0)
                    {
                        throw UnexpectedEndError();
                    }
                    Read();
                    name = ReadName();
                    SkipWhitespaces();
                    string expected = (string)elementNames.Pop();
                    xmlSpaces.Pop();
                    if (xmlSpaces.Count > 0)
                    {
                        xmlSpace = (string)xmlSpaces.Peek();
                    }
                    else
                    {
                        xmlSpace = null;
                    }
                    if (name != expected)
                    {
                        throw Error(String.Format("End tag mismatch: expected {0} but found {1}", expected, name));
                    }
                    handler.OnEndElement(name);
                    Expect('>');
                    return;

                default:                 // start tags (including empty tags)
                    HandleBufferedContent();
                    name = ReadName();
                    while (Peek() != '>' && Peek() != '/')
                    {
                        ReadAttribute(attributes);
                    }
                    handler.OnStartElement(name, attributes);
                    attributes.Clear();
                    SkipWhitespaces();
                    if (Peek() == '/')
                    {
                        Read();
                        handler.OnEndElement(name);
                    }
                    else
                    {
                        elementNames.Push(name);
                        xmlSpaces.Push(xmlSpace);
                    }
                    Expect('>');
                    return;
                }
            }
            else
            {
                ReadCharacters();
            }
        }
Пример #2
0
    public void ReadContent()
    {
        string name;

        if (IsWhitespace(Peek()))
        {
            HandleWhitespaces();
        }
        if (Peek() == '<')
        {
            Read();
            switch (Peek())
            {
            case '!':                     // declarations
                Read();
                if (Peek() == '[')
                {
                    Read();
                    if (ReadName() != "CDATA")
                    {
                        throw Error("Invalid declaration markup");
                    }
                    Expect('[');
                    ReadCDATASection();
                    return;
                }
                else if (Peek() == '-')
                {
                    ReadComment();
                    return;
                }
                else if (ReadName() != "DOCTYPE")
                {
                    throw Error("Invalid declaration markup.");
                }
                else
                {
                    ReadUntil('>', false);
                    return;
                }

            case '?':                     // PIs
                buffer.Length = 0;
                Read();
                name = ReadName();
                SkipWhitespaces();
                string text = String.Empty;
                if (Peek() != '?')
                {
                    while (true)
                    {
                        text += ReadUntil('?', false);
                        if (Peek() == '>')
                        {
                            break;
                        }
                        text += "?";
                    }
                }
                Expect('>');
                return;

            case '/':                     // end tags
                buffer.Length = 0;
                if (elementNames.Count == 0)
                {
                    throw UnexpectedEndError();
                }
                Read();
                name = ReadName();
                SkipWhitespaces();
                string expected = (string)elementNames.Pop();
                if (name != expected)
                {
                    throw Error(String.Format("End tag mismatch: expected {0} but found {1}", expected, name));
                }
                handler.OnEndElement(name);
                Expect('>');
                return;

            default:                     // start tags (including empty tags)
                buffer.Length = 0;
                name          = ReadName();
                while (Peek() != '>' && Peek() != '/')
                {
                    ReadAttribute(attributes);
                }
                SkipWhitespaces();
                if (Peek() == '/')
                {
                    handler.OnInlineElement(name, attributes);
                    Read();
                }
                else
                {
                    handler.OnStartElement(name, attributes);
                    elementNames.Push(name);
                }
                attributes.Clear();
                Expect('>');
                return;
            }
        }
        else
        {
            ReadCharacters();
        }
    }