HtmlAgilityPack using Linq for windows phone 8.1 platform

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 19:53:48

问题


As HtmlAgilityPack is yet not supported in windows phone 8.1,referencing manually in the project was a trick solution. But this is not the only problem. I could use XPath for my past project to select nodes. Now I can see that HtmlDocumentNode.SelectNode() function is no more(because of version compatibility may be).

what I used in my past project was similar to this

HtmlNode parent = document.DocumentNode.SelectSingleNode("//ul[@class='songs-list1']");
HtmlNodeCollection x = parent.ChildNodes;

I searched over stackoverflow and google and got an Idea that It's still possible to select nodes using Linq.

I'm seeking for a block of code which will work like SelectNodes, SelectNode.

Loading the HtmlDocument asynchronously would be appreciated.


回答1:


If you meant to translate your current code which using XPath to be using LINQ, then this will 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 HtmlAgilityPack version for Windows Phone 8.1 universal apps or Windows RT ("I'm seeking for a block of code which will work like SelectNodes, SelectNode"), you better don't : HtmlAgilityPack & Windows 8 Metro Apps (answer by the author of HAP).




回答2:


You can do it using 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 is assuming 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 that will get the <h6> node with class songs-list.



来源:https://stackoverflow.com/questions/25261194/htmlagilitypack-using-linq-for-windows-phone-8-1-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!