如何使用HtmlAgilityPack获取特定div的data-url的值?

时间:2016-11-12 03:52:49

标签: c# html-agility-pack

我想获得特定div的属性值,如何做到这一点。

<div id="syllabus-stages" data-featurette="async-element" data-url="/library/aspnet-mvc-forms/stages" data-strategy="update"></div>

1 个答案:

答案 0 :(得分:1)

假设您将使用HTML解析器HtmlAgilityPack(您的问题中有标记):

WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.mywebsite/mypage");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(downloadString);
//string whatUrLookingFor = doc.GetElementbyId("syllabus-stages").InnerHtml;
string whatUrLookingFor = doc.GetElementbyId("syllabus-stages").Attributes["data-url"].Value;