示例#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);
        }
示例#2
0
        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);
            }
        }