Пример #1
0
        /// <summary>
        /// Selects a list of nodes matching the <see cref="XPath"/> expression.
        /// </summary>
        /// <param name="element"></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)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }

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

            var nav    = new HtmlDocumentNavigator(element.Owner, element);
            var it     = nav.Select(xpath);
            var result = new List <INode>();

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

            return(result);
        }
Пример #2
0
        /// <summary>
        /// using the xpath 1.0 string select only a single node
        /// </summary>
        /// <param name="element"></param>
        /// <param name="xpath"></param>
        /// <returns></returns>
        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));
            }

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

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

            var node = (HtmlDocumentNavigator)it.Current;

            return(node.CurrentNode);
        }