示例#1
0
文件: Program.cs 项目: SinxHe/ForTest
        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就支持
        }
        /// <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;
        }
示例#4
0
        /// <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);
        }
示例#5
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 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;
        }
示例#6
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");
            }

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

            HtmlNodeNavigator node = (HtmlNodeNavigator)it.Current;
            return node.CurrentNode;
        }
示例#7
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 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);
        }
示例#9
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");
            }

            HtmlNodeNavigator nav = new HtmlNodeNavigator(_ownerdocument, this);
            XPathNodeIterator it  = nav.Select(xpath);

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

            HtmlNodeNavigator node = (HtmlNodeNavigator)it.Current;

            return(node.CurrentNode);
        }
示例#10
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 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>
		/// 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;
		}