Example #1
0
        private XMLElement processBlock(Match tag, ref int index)
        {
            XMLElement result = new XMLElement("");

            string tagStr = tag.Value;

            if (!RE_XML_TAG_CLOSE.Match(tagStr).Success)
            {
                var tagMatch = RE_XML_TAG_PARTS.Match(tagStr);

                string name      = tagMatch.Groups[1].Value;
                bool   selfClose = RE_XML_TAG_SELFCLOSE.Match(tagStr).Success;

                string attrs      = tagMatch.Groups[2].Value;
                var    attributes = parseAttributes(attrs);

                result.Name = name;
                result.setAttributes(attributes);

                index += tagMatch.Index + tagMatch.Length;

                if (!selfClose)
                {
                    string textPart = "";

                    bool closed = false;
                    while ((!closed) && ((tag = nextTagMatch(index)).Success))
                    {
                        textPart = m_ProcString.Substring(index, tag.Index - index);
                        result.appendText(textPart.Trim());

                        if ((tagMatch = RE_XML_TAG_CLOSE.Match(tag.Value)).Success) // if closing tag
                        {
                            if (tagMatch.Groups[1].Value == name)                   // for this entity
                            {
                                closed = true;                                      // exit
                                index  = tag.Index + tag.Length;                    // pass last position to caller
                            }
                            else                                                    // not this entity
                            {
                                throw new Exception("XMLParser error: Intersection of closing tags at symbol "
                                                    + tag.Index.ToString() + ".");
                            }
                        }
                        else    // opening tag
                        {
                            index = tag.Index;
                            result.addChild(processBlock(tag, ref index));
                        }
                    }
                }
            }
            else
            {
                throw new Exception("XMLParser error: Closing tag before opening at symbol "
                                    + tag.Index.ToString() + ".");
            }

            return(result);
        }