// Description: Html Agility Pack - HTML Parsers, selectors, traversors, manupulators.
// Website & Documentation: http://html-agility-pack.net
// Forum & Issues: https://github.com/zzzprojects/html-agility-pack
// License: https://github.com/zzzprojects/html-agility-pack/blob/master/LICENSE
// More projects: http://www.zzzprojects.com/
// Copyright © ZZZ Projects Inc. 2014 - 2017. All rights reserved.
#if !METRO
using System;
using System.Xml.XPath;
namespace HtmlAgilityPack
{
public partial class HtmlNode : IXPathNavigable
{
///
/// Creates a new XPathNavigator object for navigating this HTML node.
///
/// An XPathNavigator object. The XPathNavigator is positioned on the node from which the method was called. It is not positioned on the root of the document.
public XPathNavigator CreateNavigator()
{
return new HtmlNodeNavigator(OwnerDocument, this);
}
///
/// Creates an XPathNavigator using the root of this document.
///
///
public XPathNavigator CreateRootNavigator()
{
return new HtmlNodeNavigator(OwnerDocument, OwnerDocument.DocumentNode);
}
///
/// Selects a list of nodes matching the expression.
///
/// The XPath expression.
/// An containing a collection of nodes matching the query, or null if no node matched the XPath expression.
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, false);
}
if (list.Count == 0 && !OwnerDocument.OptionEmptyCollection)
{
return null;
}
return list;
}
///
/// Selects a list of nodes matching the expression.
///
/// The XPath expression.
/// An containing a collection of nodes matching the query, or null if no node matched the XPath expression.
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;
}
///
/// Selects the first XmlNode that matches the XPath expression.
///
/// The XPath expression. May not be null.
/// The first that matches the XPath query or a null reference if no matching node was found.
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;
}
///
/// Selects a list of nodes matching the expression.
///
/// The XPath expression.
/// An containing a collection of nodes matching the query, or null if no node matched the XPath expression.
public HtmlNode SelectSingleNode(XPathExpression 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;
}
}
}
#endif