-1

I'm trying to output a specific string from within my httpwebresponse..

currently:

string str3 = (UrlResponse);

which if I use:

Console.Write(str3);

it will output:

<html>

<head>
     <title>Hello World</title>
</head>

<body>
    Random content....
    <tr>
    <td>1</td>
    <td>2</td>
    <td>3</td>
    </tr>
</body>
</html>

but I want to just output the text within

<td>2</td>

so expected ouput:

2
Uni VPS
  • 99
  • 1
  • 9
  • 1
    Take a look at [HtmlAgilityPack](http://html-agility-pack.net/) – thisextendsthat Mar 18 '18 at 17:12
  • Sounds like you're trying to [parse html](https://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags). It would be relatively easy to do if you could isolate the tag consistently, but as you saif there is a bunch of "random content" that would be very difficult. Are you sure there isn't another way to get the data you're looking for? It's quite hard to achieve what you want. – Captain Obvious Mar 18 '18 at 17:14
  • @thisextendsthat i tried > https://stackoverflow.com/questions/49350391/htmlagilitypack-get-xpath-contents-with-httpwebrequest – Uni VPS Mar 18 '18 at 17:16
  • Do those Html Elements have some specific attribute useful to single them out somehow (even in a range of elements besides the tag)? A Class name, a Style, ID, Name, a defined position in the Dom, a type of Value/InnerText. Nothing? – Jimi Mar 18 '18 at 17:51
  • nope nothing to define them as a separate td, however I have managed to responses from using htmlagilitypack just struggling to now get xpath working, if i use select id it works but outputs every td, where as xpath returns blank – Uni VPS Mar 18 '18 at 17:54

1 Answers1

0

Here an example of getting that specific value using HtmlAgilityPack and xPath.

This xPath guide might be of some help: https://www.w3schools.com/xml/xml_xpath.asp

var str = @"<html>
<head>
        <title>Hello World</title>
</head>

<body>
    <table>
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
        </tr>
    </table>
</body>
</html>";

var document = new HtmlAgilityPack.HtmlDocument();
document.LoadHtml(str);

// returns 2                
var value = document.DocumentNode.SelectNodes("//table//tr//td[2]")[0].InnerHtml; 
Angrist
  • 147
  • 1
  • 9