/// <summary> /// 将 Element / Attribute 转换为 Element /// </summary> /// <param name="navigator"></param> /// <returns></returns> public static HtmlNodeNavigator ToElement(this HtmlNodeNavigator navigator) { // CurrentNode.CreateNavigator()? 使用的原因是因为 如果 navigator 是 Attribute, 那么SelectSingleNode()将无法寻找 Element, 如果转换成 node, 在根据 node 转换为 Element, 再进行查找的话就可以了 // Element -> Element, Attribute // Attribute -> Attribute return(navigator.CurrentNode.CreateNavigator() as HtmlNodeNavigator); }
/// <summary> /// Moves to the same position as the specified HtmlNavigator. /// </summary> /// <param name="other">The HtmlNavigator positioned on the node that you want to move to.</param> /// <returns>true if successful, otherwise false. If false, the position of the navigator is unchanged.</returns> public override bool MoveTo(XPathNavigator other) { HtmlNodeNavigator nav = other as HtmlNodeNavigator; if (nav == null) { InternalTrace(">false (nav is not an HtmlNodeNavigator)"); return(false); } InternalTrace("moveto oid=" + nav.GetHashCode() + ", n:" + nav._currentnode.Name + ", a:" + nav._attindex); if (nav._doc == _doc) { _currentnode = nav._currentnode; _attindex = nav._attindex; InternalTrace(">true"); return(true); } // we don't know how to handle that InternalTrace(">false (???)"); return(false); }
/// <summary> /// 根据 html 字符串创建 /// </summary> /// <remarks>Hesinx 2016-05-26</remarks> /// <param name="navigator">(null as HtmlNodeNavigator)</param> /// <param name="html"></param> /// <returns></returns> public static HtmlNodeNavigator Create(this HtmlNodeNavigator navigator, string html) { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(html); return(doc.CreateNavigator() as HtmlNodeNavigator); }
/// <summary>Determines whether the specified <see cref="T:System.Xml.XPath.XPathNavigator"/> is a descendant of the current <see cref="T:System.Xml.XPath.XPathNavigator"/>.</summary> /// <param name="nav">The <see cref="T:System.Xml.XPath.XPathNavigator"/> to compare to this <see cref="T:System.Xml.XPath.XPathNavigator"/>.</param> /// <returns>Returns true if the specified <see cref="T:System.Xml.XPath.XPathNavigator"/> is a descendant of the current <see cref="T:System.Xml.XPath.XPathNavigator"/>; otherwise, false.</returns> public override bool IsDescendant(XPathNavigator nav) { if (nav == null || !(nav is HtmlNodeNavigator)) { return(false); } HtmlNodeNavigator hnav = (HtmlNodeNavigator)nav; if (hnav.CurrentDocument != CurrentDocument) { return(false); } if (IsSamePosition(hnav)) { return(false); } if (hnav._attindex > -1 && hnav.CurrentNode == CurrentNode) { return(true); } HtmlNode node = hnav.CurrentNode; while (node != null) { if (node.ParentNode == CurrentNode) { return(true); } node = node.ParentNode; } return(false); }
/// <summary> /// 将当前 HtmlNodeNavigator 转化为 Root HtmlNodeNavigator /// </summary> /// <param name="navigator"></param> /// <remarks>Hesinx 2016-05-26</remarks> /// <returns></returns> public static HtmlNodeNavigator ToRoot(this HtmlNodeNavigator navigator) { string docHtml = navigator.CurrentNode.OuterHtml; HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(docHtml); return(doc.CreateNavigator() as HtmlNodeNavigator); }
public override bool IsSamePosition(XPathNavigator other) { HtmlNodeNavigator navigator = other as HtmlNodeNavigator; if (navigator == null) { return(false); } return(navigator._currentnode == _currentnode); }
/// <summary> /// Determines whether the current HtmlNavigator is at the same position as the specified HtmlNavigator. /// </summary> /// <param name="other">The HtmlNavigator that you want to compare against.</param> /// <returns>true if the two navigators have the same position, otherwise, false.</returns> public override bool IsSamePosition(XPathNavigator other) { HtmlNodeNavigator nav = other as HtmlNodeNavigator; if (nav == null) { InternalTrace(">false"); return false; } InternalTrace(">" + (nav._currentnode == _currentnode)); return (nav._currentnode == _currentnode); }
private HtmlNodeNavigator(HtmlNodeNavigator nav) { if (nav == null) { throw new ArgumentNullException(nameof(nav)); } this._doc = nav._doc; this._currentnode = nav._currentnode; this._attindex = nav._attindex; this._nametable = nav._nametable; }
public override bool MoveTo(XPathNavigator other) { HtmlNodeNavigator htmlNodeNavigator = other as HtmlNodeNavigator; if (htmlNodeNavigator == null || htmlNodeNavigator._doc != this._doc) { return(false); } this._currentnode = htmlNodeNavigator._currentnode; this._attindex = htmlNodeNavigator._attindex; return(true); }
public static void Main() { //<catalog> // <cd country="USA"> // <title>Empire Burlesque</title> // <artist>Bob Dylan</artist> // <price>10.90</price> // </cd> // <cd country="UK"> // <title>James Bond</title> // <artist>Hello World</artist> // <price>123456</price> // </cd> //</catalog> HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.Load("xml.xml"); var node = doc.DocumentNode.SelectSingleNode("/catalog/cd/@country"); // TODO : 选择的是cd元素而不是country属性 Console.WriteLine(node.GetType()); Console.WriteLine(node); HtmlAgilityPack.HtmlNodeNavigator docx = new HtmlAgilityPack.HtmlNodeNavigator("xml.xml"); var nodex = docx.SelectSingleNode("/catalog/cd/@country"); // TODO 不能使用SelectNodes var ss = nodex.SelectSingleNode("//price"); Debug.WriteLine(ss?.Value ?? "null"); nodex.MoveToRoot(); var ss2 = nodex.SelectSingleNode("//price"); Debug.WriteLine(ss2?.Value ?? "null"); nodex.MoveToFirstChild(); Console.WriteLine(nodex.GetType()); Console.WriteLine(nodex.Value); // 这个解决了属性选择的问题 var r = docx.Select("/catalog/cd/@country").Cast <HtmlNodeNavigator>(); // http://stackoverflow.com/questions/26744559/htmlagilitypack-xpath-and-regex TODO 关于 SelectNodes 的解决方案 var nodes = docx.SelectSet("/catalog/cd/@country"); //XmlDocument doc = new XmlDocument(); //doc.Load("xml.xml"); //var r = doc.DocumentElement.SelectSingleNode("//@country"); // !! XmlDocodument就支持 }
public override bool MoveTo(XPathNavigator other) { HtmlNodeNavigator navigator = other as HtmlNodeNavigator; if ((navigator != null) && (navigator._doc == _doc)) { _currentnode = navigator._currentnode; _attindex = navigator._attindex; return(true); } return(false); }
private HtmlNodeNavigator(HtmlNodeNavigator nav) { _doc = new HtmlDocument(); _nametable = new HtmlNameTable(); if (nav == null) { throw new ArgumentNullException("nav"); } _doc = nav._doc; _currentnode = nav._currentnode; _attindex = nav._attindex; _nametable = nav._nametable; }
/// <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(OwnerDocument, this); var it = nav.Select(xpath); if (!it.MoveNext()) return null; var node = (HtmlNodeNavigator)it.Current; return node.CurrentNode; }
/// <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 HtmlNodeCollection SelectNodes(string xpath) { var list = new HtmlNodeCollection(null); var nav = new HtmlNodeNavigator(OwnerDocument, this); var it = nav.Select(xpath); while (it.MoveNext()) { var n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode); } return list.Count == 0 ? null : list; }
private HtmlNodeNavigator(HtmlNodeNavigator nav) { if (nav == null) { throw new ArgumentNullException("nav"); } InternalTrace(null); _doc = nav._doc; _currentnode = nav._currentnode; _attindex = nav._attindex; _nametable = nav._nametable; // REVIEW: should we do this? }
/// <summary> /// Selects a list of nodes or attributes 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 empty collection if no node matched the XPath expression.</returns> public HtmlCollection Select(string xpath) { var list = new HtmlCollection(); HtmlNodeNavigator nav = new HtmlNodeNavigator(OwnerDocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.Current); } return(list); }
/// <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 HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection list = new HtmlNodeCollection(null); HtmlNodeNavigator nav = new HtmlNodeNavigator(OwnerDocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode); } return list; }
public HtmlNode SelectSingleNode(string xpath) { if (xpath == null) { throw new ArgumentNullException(nameof(xpath)); } XPathNodeIterator xpathNodeIterator = new HtmlNodeNavigator(this.OwnerDocument, this).Select(xpath); if (!xpathNodeIterator.MoveNext()) { return((HtmlNode)null); } return(((HtmlNodeNavigator)xpathNodeIterator.Current).CurrentNode); }
public HtmlNode SelectSingleNode(string xpath) { if (xpath == null) { throw new ArgumentNullException("xpath"); } XPathNodeIterator iterator = new HtmlNodeNavigator(OwnerDocument, this).Select(xpath); if (!iterator.MoveNext()) { return(null); } HtmlNodeNavigator current = (HtmlNodeNavigator)iterator.Current; return(current.CurrentNode); }
public HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection nodes = new HtmlNodeCollection(null); XPathNodeIterator iterator = new HtmlNodeNavigator(OwnerDocument, this).Select(xpath); while (iterator.MoveNext()) { HtmlNodeNavigator current = (HtmlNodeNavigator)iterator.Current; nodes.Add(current.CurrentNode); } if (nodes.Count == 0) { return(null); } return(nodes); }
public HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection htmlNodeCollection = new HtmlNodeCollection((HtmlNode)null); XPathNodeIterator xpathNodeIterator = new HtmlNodeNavigator(this.OwnerDocument, this).Select(xpath); while (xpathNodeIterator.MoveNext()) { HtmlNodeNavigator current = (HtmlNodeNavigator)xpathNodeIterator.Current; htmlNodeCollection.Add(current.CurrentNode); } if (htmlNodeCollection.Count == 0) { return((HtmlNodeCollection)null); } return(htmlNodeCollection); }
/// <summary> /// Determines whether the current HtmlNavigator is at the same position as the specified HtmlNavigator. /// </summary> /// <param name="other">The HtmlNavigator that you want to compare against.</param> /// <returns>true if the two navigators have the same position, otherwise, false.</returns> public override bool IsSamePosition(XPathNavigator other) { HtmlNodeNavigator nav = other as HtmlNodeNavigator; if (nav == null) { #if TRACE_NAVIGATOR InternalTrace(">false"); #endif return(false); } #if TRACE_NAVIGATOR InternalTrace(">" + (nav._currentnode == _currentnode)); #endif return(nav._currentnode == _currentnode); }
/// <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"); } HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this); XPathNodeIterator it = nav.Select(xpath); if (!it.MoveNext()) { return null; } HtmlNodeNavigator node = (HtmlNodeNavigator)it.Current; return node.CurrentNode; }
/// <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 HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection list = new HtmlNodeCollection(null); HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode); } if (list.Count == 0) { return(null); } return(list); }
/// <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 List <IHtmlBaseNode> SelectNodes(string xpath) { List <IHtmlBaseNode> list = new List <IHtmlBaseNode>(); HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode); } //if (list.Count == 0) //{ // return null; //} return(list); }
/// <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"); } HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this); XPathNodeIterator it = nav.Select(xpath); if (!it.MoveNext()) { return(null); } HtmlNodeNavigator node = (HtmlNodeNavigator)it.Current; return(node.CurrentNode); }
/// <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 HtmlNodeCollection SelectNodes(XPathExpression xpath) { HtmlNodeCollection list = new HtmlNodeCollection(null); HtmlNodeNavigator nav = new HtmlNodeNavigator(OwnerDocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode, false); } if (list.Count == 0 && !OwnerDocument.OptionEmptyCollection) { return(null); } return(list); }
/// <summary> /// 根据 xpath 选择单个 node, 非 root node /// </summary> /// <param name="navigator"></param> /// <param name="xpath"></param> /// <returns></returns> public static HtmlNodeNavigator SelectSingle(this HtmlNodeNavigator navigator, string xpath) { return(navigator.ToElement().SelectSingleNode(xpath) as HtmlNodeNavigator); }
/// <summary> /// 根据 xpath 选择 node 集合, node 非 root node /// </summary> /// <remarks>Hesinx 2016-05-26</remarks> /// <param name="navigator"></param> /// <param name="xpath"></param> /// <returns></returns> public static IEnumerable <HtmlNodeNavigator> SelectSet(this HtmlNodeNavigator navigator, string xpath) { return(navigator.ToElement().Select(xpath).Cast <HtmlNodeNavigator>()); //return from HtmlNodeNavigator m in navigator.Select(xpath) select m; }
/// <summary> /// 根据 xpath 选择首个 node, node 被转化为 root node /// </summary> /// <remarks>Hesinx 2016-05-26</remarks> /// <param name="navigator"></param> /// <param name="xpath"></param> /// <returns></returns> public static HtmlNodeNavigator SelectSingleAsRoot(this HtmlNodeNavigator navigator, string xpath) { return(navigator.SelectSingle(xpath).ToRoot()); }
/// <summary> /// Selects a list of nodes matching the XPath expression. /// </summary> /// <param name="xpath">The XPath expression.</param> /// <returns>An HtmlNodeCollection containing a collection of nodes matching the XPath query, or null if no node matched the XPath expression.</returns> public HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection list = new HtmlNodeCollection(null); try { HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this); XPathNodeIterator it = nav.Select(xpath); while (it.MoveNext()) { HtmlNodeNavigator n = (HtmlNodeNavigator)it.Current; list.Add(n.CurrentNode); } } catch (Exception ex) { O2.Kernel.ExtensionMethods.Logging_ExtensionMethods.log(ex, "in HtmlNodeCollection SelectNodes"); } //DC removed this so that we get an empty list when there are no matching nodes //if (list.Count == 0) //{ // return null; //} return list; }
/// <summary> /// 根据 xpath 选择 node, 并将当前 node 转化为 root node /// </summary> /// <remarks>Hesinx 2016-05-26</remarks> /// <param name="navigator"></param> /// <param name="xpath"></param> /// <returns></returns> public static IEnumerable <HtmlNodeNavigator> SelectSetAsRoot(this HtmlNodeNavigator navigator, string xpath) { return(navigator.SelectSet(xpath).Select(m => m.ToRoot())); }
public override bool IsSamePosition(XPathNavigator other) { HtmlNodeNavigator htmlNodeNavigator = other as HtmlNodeNavigator; return(htmlNodeNavigator != null && htmlNodeNavigator._currentnode == this._currentnode); }
/// <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="T:HtmlAgilityPack.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"); XPathNodeIterator xpathNodeIterator = new HtmlNodeNavigator(this.OwnerDocument, this).Select(xpath); if (!xpathNodeIterator.MoveNext()) return (HtmlNode) null; return ((HtmlNodeNavigator) xpathNodeIterator.Current).CurrentNode; }
/// <summary> /// Selects a list of nodes matching the <see cref="P:HtmlAgilityPack.HtmlNode.XPath"/> expression. /// /// </summary> /// <param name="xpath">The XPath expression.</param> /// <returns> /// An <see cref="T:HtmlAgilityPack.HtmlNodeCollection"/> containing a collection of nodes matching the <see cref="P:HtmlAgilityPack.HtmlNode.XPath"/> query, or <c>null</c> if no node matched the XPath expression. /// </returns> public HtmlNodeCollection SelectNodes(string xpath) { HtmlNodeCollection htmlNodeCollection = new HtmlNodeCollection((HtmlNode) null); XPathNodeIterator xpathNodeIterator = new HtmlNodeNavigator(this.OwnerDocument, this).Select(xpath); while (xpathNodeIterator.MoveNext()) { HtmlNodeNavigator htmlNodeNavigator = (HtmlNodeNavigator) xpathNodeIterator.Current; htmlNodeCollection.Add(htmlNodeNavigator.CurrentNode); } if (htmlNodeCollection.Count == 0) return (HtmlNodeCollection) null; return htmlNodeCollection; }