예제 #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);
        }
예제 #2
0
        public XMLElement parse(string inputString)
        {
            this.m_ProcString = clearInput(inputString);

            XMLElement doc = processDocument();

            return(doc);
        }
예제 #3
0
        public bool addChild(XMLElement newChild)
        {
            bool result = false;

            m_Children.Add(newChild);
            result = true;

            return(result);
        }
예제 #4
0
        private XMLElement processDocument()
        {
            XMLElement doc = new XMLElement("xml");

            var match = RE_XML_ROOT.Match(m_ProcString);    // check prologue

            if ((!match.Success) || (match.Index > 0))
            {
                throw new Exception("Not a valid XML prologue.");
            }

            int startIndex = match.Index + match.Length;

            Match rootTag = nextTagMatch(startIndex);

            startIndex = rootTag.Index;

            XMLElement root = processBlock(rootTag, ref startIndex);

            doc.addChild(root);

            return(doc);
        }