GetPreviousSibling() public static method

Finds the previous sibling element of the given XmlElement skipping any intermediate non-element XmlNode instances.
public static GetPreviousSibling ( XmlElement element ) : XmlElement
element System.Xml.XmlElement The context .
return System.Xml.XmlElement
Exemplo n.º 1
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);
        }
Exemplo n.º 2
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("");
        }