HtmlAgilityPack uses Linq for Windows Phone 8.1 platform
Since HtmlAgilityPack is not yet supported on Windows Phone 8.1, linking manually in the project was a tricky solution. But this is not the only problem. I could use XPath
for my past project to select nodes. Now I see that the function HtmlDocumentNode.SelectNode()
no longer exists (due to version compatibility).
what i used in my past project was like this
HtmlNode parent = document.DocumentNode.SelectSingleNode("//ul[@class='songs-list1']");
HtmlNodeCollection x = parent.ChildNodes;
I searched stackoverflow and google and got the Idea that it is still possible to select nodes using Linq.
I'm looking for a block of code that will work as a SelectNodes
, SelectNode
.
Loading is asynchronous HtmlDocument
.
source to share
If you want to translate your current code that uses XPath to use LINQ then this would do:
HtmlNode parent = document.DocumentNode
.Descendants("ul")
.FirstOrDefault(o => o.GetAttributeValue("class", "")
== "songs-list1")
HtmlNodeCollection x = parent.ChildNodes;
But if you expect to find methods that accept XPath in the Windows Phone 8.1 or Windows RT Universal Apps version of HtmlAgilityPack ("I'm looking for a block of code that will work like SelectNodes
, SelectNode
") you better not do: HtmlAgilityPack and Windows 8 Metro Apps (author's answer HAP).
source to share
You can do this with the Element / s method:
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(htmlString);
var h6Nodes = from h6element in doc.DocumentNode.Element("body").Element("center").Elements("h6")
where h6element.Attributes["class"].Value.Equals("songs-list")
select h6element;
This assumes you have something like
string htmlString = @"<html>
<body>
<center>
<h6>Hello </h6>
<h6>World! </h6>
<h6 class=""songs-list"">
Insert that one song here
</h6>
</center>
</body>
</html>"
and you get a <h6>
node with a class-list of songs.
source to share