Ejemplo n.º 1
0
        /// <summary>
        /// Selects a single node (or returns null) matching the <see cref="XPath"/> expression.
        /// </summary>
        /// <param name="element">The element to start looking from.</param>
        /// <param name="xpath">The XPath expression.</param>
        /// <returns>The node matching <paramref name="xpath"/> query, if any.</returns>
        /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
        public static INode SelectSingleNode(this IElement element, String xpath)
        {
            var el  = element ?? throw new ArgumentNullException(nameof(element));
            var xp  = xpath ?? throw new ArgumentNullException(nameof(xpath));
            var nav = new HtmlDocumentNavigator(el.Owner, el);
            var it  = nav.Select(xp);

            if (it.MoveNext())
            {
                var node = (HtmlDocumentNavigator)it.Current;
                return(node.CurrentNode);
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Selects a list of nodes matching the <see cref="XPath"/> expression.
        /// </summary>
        /// <param name="element">The element to start looking from.</param>
        /// <param name="xpath">The XPath expression.</param>
        /// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
        /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
        public static List <INode> SelectNodes(this IElement element, String xpath)
        {
            var el     = element ?? throw new ArgumentNullException(nameof(element));
            var xp     = xpath ?? throw new ArgumentNullException(nameof(xpath));
            var nav    = new HtmlDocumentNavigator(el.Owner, el);
            var it     = nav.Select(xp);
            var result = new List <INode>();

            while (it.MoveNext())
            {
                var naviagtor = (HtmlDocumentNavigator)it.Current;
                var e         = naviagtor.CurrentNode;
                result.Add(e);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Selects a list of nodes matching the <see cref="XPath"/> expression.
        /// </summary>
        /// <param name="element">The element to start looking from.</param>
        /// <param name="xpath">The XPath expression.</param>
        /// <param name="resolver"></param>
        /// <param name="ignoreNamespaces"></param>
        /// <returns>List of nodes matching <paramref name="xpath"/> query.</returns>
        /// <exception cref="ArgumentNullException">Throws if <paramref name="element"/> or <paramref name="xpath"/> is <c>null</c></exception>
        public static List <INode> SelectNodes(this IElement element, string xpath, IXmlNamespaceResolver resolver, bool ignoreNamespaces = true)
        {
            var el     = element ?? throw new ArgumentNullException(nameof(element));
            var xp     = xpath ?? throw new ArgumentNullException(nameof(xpath));
            var nav    = new HtmlDocumentNavigator(el.Owner, el, ignoreNamespaces);
            var it     = nav.Select(xp, resolver ?? new XmlNamespaceManager(new NameTable()));
            var result = new List <INode>();

            while (it.MoveNext())
            {
                // ReSharper disable once IdentifierTypo
                var naviagtor = (HtmlDocumentNavigator)it.Current;
                // ReSharper disable once PossibleNullReferenceException
                var e = naviagtor.CurrentNode;
                result.Add(e);
            }

            return(result);
        }
Ejemplo n.º 4
0
        public static INode SelectSingleNode(this IElement element, string xpath)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

            if (xpath == null)
            {
                throw new ArgumentNullException(nameof(xpath));
            }

            HtmlDocumentNavigator nav = new HtmlDocumentNavigator(element.Owner, element);
            XPathNodeIterator     it  = nav.Select(xpath);

            if (!it.MoveNext())
            {
                return(null);
            }

            HtmlDocumentNavigator node = (HtmlDocumentNavigator)it.Current;

            return(node.CurrentNode);
        }