Reputation: 705
Consider the following HTML
<li class="">
<a href="/package/tar/v/5.0.6" class="_132722c7 f5 black-60 lh-copy code link underline-hover" title="5.0.6">5.0.6</a>
<div class="c440844e mh2 h1"></div>
<code class="downloads">65</code>
<ul class="c495d29d list ml0 pl0 _8aa9368d">
<li>
<div class="c440844e mh2 h1"></div>
<code class="ccbecba3 f5 black-60 lh-copy">
<time datetime="2021-07-23T22:44:40.117Z" title="24-7-2021 00:44:40">3 months ago</time>
</code>
</li>
</ul>
</li>
I want to get the first 4 characters of the datetime attrbiute of the time element. I found this specific li element by the following code.
htmlDoc2.DocumentNode.SelectSingleNode("//a[@title='"+ entry.Value + "']").ParentNode
I tried getting the attribute with the following code but it doesn't work.
var iets = htmlDoc2.DocumentNode.SelectSingleNode("//a[@title='"+ entry.Value + "']").ParentNode
.SelectSingleNode("//time").Attributes["datetime"].Value.Substring(0,4);
But when running this "iets" will just return the datetime attribute of the first time element in the ul list. How can I change it so that it will actually get the time attribute of the ParentNode?
Upvotes: 0
Views: 64
Reputation: 387
Try this. I took the XPath of the parent in the inner select and added the required element to the outer select.
Please note this works on the assumption that the title is unique, or you will always get the first match.
htmlDoc2.DocumentNode.SelectSingleNode(
$"{htmlDoc2.DocumentNode.
SelectSingleNode("//a[@title='"+ entry.Value +"']")
.ParentNode.XPath}//time")
.Attributes["datetime"].Value.Substring(0, 4)
Upvotes: 1