Esempio n. 1
0
        /// <summary>
        /// Recursive search of the XMLTree for a given tag.
        /// Result:
        ///
        /// &lt;result&gt; DATA &lt;/result&gt;
        ///
        ///
        /// </summary>
        /// <param name="tagname"></param>
        /// <returns></returns>
        public XmlTree Search(string tagname)
        {
            XmlTree tree = new XmlTree();

            tree.TagName = "result";
            foreach (XmlTree subtree in this.ChildTrees)
            {
                if (subtree.TagName.Equals(tagname))
                {
                    tree.ChildTrees.Add(subtree);
                }

                XmlTree result = subtree.Search(tagname);
                tree.ChildTrees.AddRange(result.ChildTrees);
                tree.ChildNodes.AddRange(result.ChildNodes);
            }
            foreach (SimpleXmlTag tag in this.ChildNodes)
            {
                if (tag.TagName.Equals(tagname))
                {
                    tree.ChildNodes.Add(tag);
                }
            }

            return(tree);
        }
Esempio n. 2
0
        /// <summary>
        /// Merge two xmltags
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public XmlTree MergeWith(XmlTree other)
        {
            bool merge = true;

            if (this.TagName.Equals(other.TagName))
            {
                foreach (string attr in this.Attributes.Keys)
                {
                    if (other.Attributes.ContainsKey(attr) && !this.Attributes[attr].Equals(other.Attributes[attr]))
                    {
                        merge = false;
                    }
                }
            }
            else
            {
                merge = false;
            }

            if (merge)
            {
                XmlTree tree = new XmlTree()
                {
                    TagName = this.TagName
                };
                foreach (string attr in this.Attributes.Keys)
                {
                    tree.Attributes.Add(attr, this.Attributes[attr]);
                }
                foreach (string attr in other.Attributes.Keys)
                {
                    if (!tree.Attributes.ContainsKey(attr))
                    {
                        tree.Attributes.Add(attr, other.Attributes[attr]);
                    }
                }

                tree.ChildNodes.AddRange(this.ChildNodes);
                tree.ChildNodes.AddRange(other.ChildNodes);
                tree.ChildTrees.AddRange(this.ChildTrees);
                tree.ChildTrees.AddRange(other.ChildTrees);

                return(tree);
            }
            else if (this.TagName.Equals("merge"))
            {
                XmlTree tree = new XmlTree()
                {
                    TagName = "merge"
                };
                tree.ChildTrees.AddRange(this.ChildTrees);
                tree.ChildNodes.AddRange(this.ChildNodes);
                tree.ChildTrees.Add(other);
                return(tree);
            }
            else if (other.TagName.Equals("merge"))
            {
                XmlTree tree = new XmlTree()
                {
                    TagName = "merge"
                };
                tree.ChildTrees.AddRange(other.ChildTrees);
                tree.ChildNodes.AddRange(other.ChildNodes);
                tree.ChildTrees.Add(this);
                return(tree);
            }
            else
            {
                XmlTree tree = new XmlTree()
                {
                    TagName = "merge"
                };

                tree.ChildTrees.Add(this);
                tree.ChildTrees.Add(other);
                return(tree);
            }
        }