HtmlAgilityPack은 Windows Phone 8.1에서 아직 지원되지 않으므로 프로젝트에서 수동으로 참조하는 것은 트릭 솔루션이었습니다. 그러나 이것이 유일한 문제는 아닙니다. 과거 프로젝트에서 XPath
를 사용하여 노드를 선택할 수있었습니다. 이제 HtmlDocumentNode.SelectNode()
함수가 더 이상 버전 호환성이있을 수 있으므로 볼 수 있습니다.
과거 프로젝트에서 사용한 것과 비슷한 것이 었습니다.
HtmlNode parent = document.DocumentNode.SelectSingleNode("//ul[@class='songs-list1']");
HtmlNodeCollection x = parent.ChildNodes;
나는 stackoverflow와 구글을 통해 검색하고 Linq를 사용하여 노드를 선택할 수 있다는 아이디어를 얻었다.
SelectNodes
처럼 작동하는 코드 블록을 찾고 있습니다. SelectNode
.
HtmlDocument
비동기 적으로로드하는 것이 좋습니다.
XPath를 사용하여 LINQ를 사용하는 현재 코드를 변환하려는 경우 다음을 수행합니다.
HtmlNode parent = document.DocumentNode
.Descendants("ul")
.FirstOrDefault(o => o.GetAttributeValue("class", "")
== "songs-list1")
HtmlNodeCollection x = parent.ChildNodes;
그러나 Windows Phone 8.1 범용 앱 또는 Windows RTX 용 HtmlAgilityPack 버전에서 XPath를 수락하는 메서드를 찾으려면 ( " SelectNodes
, SelectNode
와 같이 작동하는 코드 블록을 찾고 있습니다.) HtmlAgilityPack & Windows 8 Metro Apps (HAP 작성자의 답변).
Element / s 메서드를 사용하여이 작업을 수행 할 수 있습니다.
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;
이것은 당신이 뭔가를 가지고 있다고 가정하고 있습니다.
string htmlString = @"<html>
<body>
<center>
<h6>Hello </h6>
<h6>World! </h6>
<h6 class=""songs-list"">
Insert that one song here
</h6>
</center>
</body>
</html>"
그러면 클래스 곡 목록이있는 <h6>
노드가 생성됩니다.