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);
        }
Example #2
0
        public XMLElement parse(string inputString, bool checkPrologue = false)
        {
            this.m_ProcString = clearInput(inputString);

            XMLElement doc = processDocument(checkPrologue);

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

            m_Children.Add(newChild);
            result = true;

            return(result);
        }
Example #4
0
        private string getBlock(XMLElement node)
        {
            string result = "<" + node.Name;

            if (node.hasAttributes())
            {
                foreach (var attr in node.getAttributesList())
                {
                    result += " " + attr.Name + "=" + "\"" + attr.Value + "\"";
                }
            }

            if (!(node.hasText() || node.hasChildren()))
            {
                result += "/>";
            }
            else
            {
                result += ">";

                if (node.hasText())
                {
                    result += node.getText();
                }

                if (node.hasChildren())
                {
                    foreach (var child in node.getChildrenList())
                    {
                        result += getBlock(child);
                    }
                }

                result += "</" + node.Name + ">";
            }

            return(result);
        }
Example #5
0
        private XMLElement processDocument(bool checkPrologue = false)
        {
            int startIndex = 0;

            if (checkPrologue)
            {
                var match = RE_XML_ROOT.Match(m_ProcString);    // check prologue
                if ((!match.Success) || (match.Index > 0))
                {
                    Console.WriteLine("Invalid XML prologue.");
                }

                startIndex = match.Index + match.Length;
            }

            Match rootTag = nextTagMatch(startIndex);

            startIndex = rootTag.Index;

            XMLElement doc = processBlock(rootTag, ref startIndex);

            return(doc);
        }
Example #6
0
        public string assemble(XMLElement root)
        {
            string result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + getBlock(root);

            return(result);
        }