Exemple #1
0
 /// <summary>
 /// Selects a list of nodes matching the <see cref="XPath"/> expression.
 /// </summary>
 /// <param name="xpath">
 /// The XPath expression. 
 /// </param>
 /// <returns>
 /// An <see cref="HtmlNodeCollection"/> containing a collection of nodes matching the <see cref="XPath"/> query, or <c>null</c> if no node matched the XPath expression. 
 /// </returns>
 public IEnumerable<HtmlNode> SelectNodes(string xpath)
 {
     var nav = new HtmlNodeNavigator(this.OwnerDocument, this);
     XPathNodeIterator it = nav.Select(xpath);
     while (it.MoveNext())
     {
         var n = (HtmlNodeNavigator)it.Current;
         if (n != null)
         {
             yield return n.CurrentNode;
         }
     }
 }
Exemple #2
0
        /// <summary>
        /// Selects the first XmlNode that matches the XPath expression.
        /// </summary>
        /// <param name="xpath">
        /// The XPath expression. May not be null. 
        /// </param>
        /// <returns>
        /// The first <see cref="HtmlNode"/> that matches the XPath query or a null reference if no matching node was found. 
        /// </returns>
        public HtmlNode SelectSingleNode(string xpath)
        {
            if (xpath == null)
            {
                throw new ArgumentNullException("xpath");
            }

            var nav = new HtmlNodeNavigator(this.OwnerDocument, this);
            XPathNodeIterator it = nav.Select(xpath);
            if (!it.MoveNext())
            {
                return null;
            }

            var node = (HtmlNodeNavigator)it.Current;
            if (node != null)
            {
                return node.CurrentNode;
            }

            return null;
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="HtmlNodeNavigator"/> class.
        /// </summary>
        /// <param name="nav">
        /// The nav.
        /// </param>
        private HtmlNodeNavigator(HtmlNodeNavigator nav)
        {
            if (nav == null)
            {
                throw new ArgumentNullException("nav");
            }

            this.htmlDocument = nav.htmlDocument;
            this.currentnode = nav.currentnode;
            this.attindex = nav.attindex;
            this.htmlNameTable = nav.htmlNameTable; // REVIEW: should we do this?
        }