Reputation: 351
Firstly, I tried a lot of ways but I couldn't solve my problem. I don't know how to place my node way in SelectSingleNode(?) method. I create a html path to reach my node in my c# code but if I run this code, I take NullReferenceException because of my html path. I just want you that how can I create my html way or any other solution?
This is example of html code:
<html>
<body>
<div id="container">
<div id="box">
<div class="box">
<div class="boxContent">
<div class="userBox">
<div class="userBoxContent">
<div class="userBoxElement">
<ul id ="namePart">
<li>
<span class ="namePartContent>
</span>
</li>
</ul>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>
And this my C# code:
namespace AgilityTrial
{
class Program
{
static void Main(string[] args)
{
Uri url = new Uri("https://....");
WebClient client = new WebClient();
client.Encoding = Encoding.UTF8;
string html = client.DownloadString(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
string path = @"//html/body/div[@id='container']/div[@id='classifiedDetail']"+
"/div[@class='classifiedDetail']/div[@class='classifiedDetailContent']"+
"/div[@class='classifiedOtherBoxes']/div[@class='classifiedUserBox']"+
"/div[@class='classifiedUserContent']/ul[@id='phoneInfoPart']/li"+
"/span[@class='pretty-phone-part show-part']";
var tds = doc.DocumentNode.SelectSingleNode(path);
var date = tds.InnerHtml;
Console.WriteLine(date);
}
}
}
Upvotes: 2
Views: 67
Reputation: 8392
Take as an example your namePartContent
span node. If you want to fetch that data you would simply do this:
doc.DocumentNode.SelectSingleNode(".//span[@class='namePartContent']")?.InnerText;
It will search/fetch a single span node with namePartContent
as its class, begining at the root node, in your case <html>
;
Upvotes: 1