HtmlAgilityPack.HtmlNode.SelectSingleNode C# (CSharp) Method

SelectSingleNode() public method

Selects the first XmlNode that matches the XPath expression.
public SelectSingleNode ( string xpath ) : HtmlNode
xpath string The XPath expression. May not be null.
return HtmlNode
		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;
		}

Usage Example

        public Segment GetSegment(HtmlNode segmentDiv, string day)
        {
            var imgDiv = segmentDiv.SelectSingleNode("./div[@class='programListContentImgArea']");
            var txtDiv = segmentDiv.SelectSingleNode("./div[@class='programListContentTextArea scheduleColumnTextArea']");

            var anchor = txtDiv.GetChildById("hypProgramTxt");
            var authorNode = txtDiv.GetChildById("lblAuthor");
            var imageNode = imgDiv.SelectSingleNode(".//img");

            //TODO: Move url building (below) into UrlBuilder class

            var segment = new Segment
            {
                DayDescription = day,
                Title = anchor.DecodeHtml(),
                SegmentDetailUrl = configurationManager.BookTvDomain + anchor.GetAttributeValue("href", ""),
                Time = txtDiv.GetChildById("lblAirTime").DecodeHtml(),
                Duration = txtDiv.GetChildById("lblLength").DecodeHtml(),
                Author = (authorNode == null) ? "" : authorNode.DecodeHtml(),
                ImageUrl = configurationManager.BookTvDomain + imageNode.Attributes["src"].Value,
            };

            segment.DurationInMinutes = durationParser.GetDurationInMinutes(segment.Duration);
            segment.Date = dayParser.Parse(segment.DayDescription);

            return segment;
        }
All Usage Examples Of HtmlAgilityPack.HtmlNode::SelectSingleNode