The DOM class contains utility functions that operate on a DOM tree, often combining several simpler operations or returning a more useful type.
예제 #1
0
        /// <summary>
        /// Moves the context to the parent <see cref="XmlElement"/> if one exists.
        /// </summary>
        /// <returns><c>true</c> if the context <see cref="XmlElement"/> was changed,
        /// <c>false</c> otherwise.</returns>
        public bool MoveToParent()
        {
            XmlElement parent = DOM.GetParent(context);

            if (parent != null)
            {
                context = parent;
                return(true);
            }
            return(false);
        }
예제 #2
0
        /// <summary>
        /// Moves the context to the previous sibling <see cref="XmlElement"/>
        /// if one exists.
        /// </summary>
        /// <returns><c>true</c> if the context <c>XmlElement</c> was changed,
        /// <c>false</c> otherwise.</returns>
        public bool MoveToPreviousSibling()
        {
            XmlElement element = DOM.GetPreviousSibling(context);

            if (element != null)
            {
                context = element;
                return(true);
            }
            return(false);
        }
예제 #3
0
        /// <summary>
        /// Moves the context to the last child <see cref="XmlElement"/> if one
        /// exists.
        /// </summary>
        /// <returns><c>true</c> if the context <see cref="XmlElement"/> was
        /// changed, <c>false</c> otherwise.</returns>
        public bool MoveToLastChild()
        {
            XmlElement child = DOM.GetLastChild(context);

            if (child != null)
            {
                context = child;
                return(true);
            }
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Constructs an XPath expression that describes the location of the
        /// given <see cref="XmlNode"/> within its document tree.
        /// </summary>
        /// <param name="node">The <see cref="XmlNode"/> to be described.</param>
        /// <returns>An XPath expression describing the location of the
        /// <see cref="XmlNode"/>.</returns>
        public static string ForNode(XmlNode node)
        {
            if (node != null)
            {
                switch (node.NodeType)
                {
                case XmlNodeType.Attribute:
                    return(ForNode((node as XmlAttribute).OwnerElement) + "/@" + node.Name);

                case XmlNodeType.Element:
                {
                    int succ = 0;
                    int pred = 0;

                    for (XmlElement temp = DOM.GetPreviousSibling(node as XmlElement); temp != null;)
                    {
                        if (!temp.Name.Equals(node.Name))
                        {
                            break;
                        }

                        temp = DOM.GetPreviousSibling(temp);
                        ++succ;
                    }

                    for (XmlElement temp = DOM.GetNextSibling(node as XmlElement); temp != null;)
                    {
                        if (!temp.Name.Equals(node.Name))
                        {
                            break;
                        }

                        temp = DOM.GetNextSibling(temp);
                        ++pred;
                    }

                    if ((succ + pred) > 0)
                    {
                        return(ForNode(node.ParentNode) +
                               "/" + node.LocalName + "[" + (succ + 1) + "]");
                    }
                    else
                    {
                        return(ForNode(node.ParentNode) +
                               "/" + node.LocalName);
                    }
                }
                }
            }
            return("");
        }
예제 #5
0
        //---------------------------------------------------------------------------

        /// <summary>
        /// Evaluates a simple multiple valued path access from the given context
        /// node to the named child elements. The '*', '.' and '..' specifiers are
        /// supported.
        /// </summary>
        /// <param name="context">The context <see cref="XmlElement"/>.</param>
        /// <param name="name">The name of the required child.</param>
        /// <returns>A possibly empty <see cref="XmlNodeList"/> of matching
        /// child elements.</returns>
        public static XmlNodeList Paths(XmlElement context, string name)
        {
            if (context != null)
            {
                if (name.Equals("*"))
                {
                    return((context != null) ? DOM.GetChildElements(context) : MutableNodeList.EMPTY);
                }
                else if (name.Equals("."))
                {
                    if (context != null)
                    {
                        MutableNodeList list = new MutableNodeList();

                        list.Add(context);
                        return(list);
                    }
                    else
                    {
                        return(MutableNodeList.EMPTY);
                    }
                }
                else if (name.Equals(".."))
                {
                    if ((context != null) && (context.ParentNode != null))
                    {
                        MutableNodeList list = new MutableNodeList();

                        list.Add(context.ParentNode);
                        return(list);
                    }
                    else
                    {
                        return(MutableNodeList.EMPTY);
                    }
                }

                return(DOM.GetElementsByLocalName(context, name));
            }
            return(MutableNodeList.EMPTY);
        }
예제 #6
0
 /// <summary>
 /// Determines if the context <see cref="XmlElement"/> has at least one
 /// child element.
 /// </summary>
 /// <returns><c>true</c> if the context <see cref="XmlElement"/> has at least
 /// one child <see cref="XmlElement"/>, <c>false</c> otherwise.</returns>
 public bool HasChildNodes()
 {
     return(DOM.HasChildNodes(context));
 }