[Solved] Find Text between Html Tags [closed]


Use the HTML Agility Pack, which includes a DOM parser – it is never worth writing your own parser or RegExs for HTML.

http://www.nuget.org/packages/HtmlAgilityPack

In the example below, you can see how easy it is to select an element using XPATH. Because the values you want aren’t actually in an element, I’m using text() to get them.

If this were part of a larger document, you would expand the XPATH to reflect their location in the wider document.

string html = @"<span>Location:</span><br/>                                 
    50 Airport Road<br/>
    Ottawa, CA <br/><br/>
    <span>Latitude / Longitude:</span><br/>
    40.32083 / -71.67275<br/><br/>";

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);

// Getting an element (Location:)
var latLongLabel = doc.DocumentNode.SelectSingleNode("span[2]");

// The location is split over two text nodes
var locationValA = doc.DocumentNode.SelectSingleNode("text()[1]").InnerHtml.Trim();
var locationValB = doc.DocumentNode.SelectSingleNode("text()[2]").InnerHtml.Trim();

// The lat long
var latLongVal = doc.DocumentNode.SelectSingleNode("text()[4]").InnerHtml.Trim();

2

solved Find Text between Html Tags [closed]