I'm using a function to get all the avaliable XPath expression from an HTML, using HtmlAgilityPack library.
The problem is that I get expressions with this format:
/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/h4[1]/a[1]
I would improve it to get also the names of the nodes/elements, like this:
/html/body/div[@class='infolinks']/div[@class='music']/div[@class='item']/div[@class='release']/h4[1]/a[@title]
But I don't know how to properly get their names with HtmlAgilityPack.
How I could do it?.
Note: I'm not any XPath expert sorry if the syntax of the XPaths are bad or I missunderstand things.
The webpage sourcecode that I'm trying:
<div class="infolinks"><input type="hidden" name="IL_IN_TAG" value="1"/></div><div id="main">
<div class="music">
<h2 class="boxtitle">New releases \ <small>
<a href="/newalbums" title="New releases mp3 downloads" rel="bookmark">see all</a></small>
</h2>
<div class="item">
<div class="thumb">
<a href="http://www.mp3crank.com/curt-smith/deceptively-heavy-121861" rel="bookmark" lang="en" title="Curt Smith - Deceptively Heavy album downloads"><img width="100" height="100" alt="Mp3 downloads Curt Smith - Deceptively Heavy" title="Free mp3 downloads Curt Smith - Deceptively Heavy" src="http://www.mp3crank.com/cover-album/Curt-Smith-Deceptively-Heavy-400x400.jpg"/></a>
</div>
<div class="release">
<h3>Curt Smith</h3>
<h4>
<a href="http://www.mp3crank.com/curt-smith/deceptively-heavy-121861" title="Mp3 downloads Curt Smith - Deceptively Heavy">Deceptively Heavy</a>
</h4>
<script src="/ads/button.js"></script>
</div>
<div class="release-year">
<p>Year</p>
<span>2013</span>
</div>
<div class="genre">
<p>Genre</p>
<a href="http://www.mp3crank.com/genre/indie" rel="tag">Indie</a><a href="http://www.mp3crank.com/genre/pop" rel="tag">Pop</a>
</div>
</div>
<div class="item">
<div class="thumb">
<a href="http://www.mp3crank.com/wolf-eyes/lower-demos-121866" rel="bookmark" lang="en" title="Wolf Eyes - Lower Demos album downloads"><img width="100" height="100" alt="Mp3 downloads Wolf Eyes - Lower Demos" title="Free mp3 downloads Wolf Eyes - Lower Demos" src="http://www.mp3crank.com/cover-album/Wolf-Eyes-–-Lower-Demos.jpg" /></a>
</div>
<div class="release">
<h3>Wolf Eyes</h3>
<h4>
<a href="http://www.mp3crank.com/wolf-eyes/lower-demos-121866" title="Mp3 downloads Wolf Eyes - Lower Demos">Lower Demos</a>
</h4>
<script src="/ads/button.js"></script>
</div>
<div class="release-year">
<p>Year</p>
<span>2013</span>
</div>
<div class="genre">
<p>Genre</p>
<a href="http://www.mp3crank.com/genre/rock" rel="tag">Rock</a>
</div>
</div>
</div>
</div>
The function to get XPaths:
Public Function GetXPaths(ByVal Document As HtmlAgilityPack.HtmlDocument) As List(Of String)
Dim XPathList As New List(Of String)
Dim XPath As String = String.Empty
For Each Child As HtmlAgilityPack.HtmlNode In Document.DocumentNode.ChildNodes
If Child.NodeType = HtmlAgilityPack.HtmlNodeType.Element Then
GetXPaths(Child, XPathList, XPath)
End If
Next ' child'
Return XPathList
End Function
Private Sub GetXPaths(ByVal Node As HtmlAgilityPack.HtmlNode,
ByRef XPathList As List(Of String),
Optional ByVal XPath As String = Nothing)
XPath = Node.XPath
If Not XPathList.Contains(XPath) Then
XPathList.Add(XPath)
End If
For Each Child As HtmlAgilityPack.HtmlNode In Node.ChildNodes
If Child.NodeType = HtmlAgilityPack.HtmlNodeType.Element Then
GetXPaths(Child, XPathList, XPath)
End If
Next ' child
End Sub
And these are the XPaths that I use to retrieve some values, I would like to get more or less the same XPath fully-qualified representation on the function above.
Title = node.SelectSingleNode(".//div[@class='release']/h4/a[@title]").GetAttributeValue("title", "Unknown Title")
Cover = node.SelectSingleNode(".//div[@class='thumb']/a/img[@src]").GetAttributeValue("src", String.Empty)
Year = node.SelectSingleNode(".//div[@class='release-year']/span").InnerText
Genres = (From genre In node.SelectNodes(".//div[@class='genre']/a") Select genre.InnerText).ToArray
URL = node.SelectSingleNode(".//div[@class='release']/h4/a[@href]").GetAttributeValue("href", "Unknown URL")