[英]How to get all HTML tags that contains specific string in their attribute values using Html Agility Pack?
我正在努力獲取包含以下代碼中的特定字符串的所有HTML標記
<meta name="DCSext.oo_market" content="en-us">
<a href="http://office.microsoft.com/en-us/support/" title="Find help for Word">
<a href="http://windows.microsoft.com/en-us/windows-live/microsoft-account-help#microsoft-account=tab1" title="Microsoft Account">
我想要在其屬性中包含“en-us”的所有標簽意味着我的輸出應該返回所有上述html標簽。 誰能幫助我如何使用HTML Agility Pack來幫助它?
您可以使用以下XPath //*[@*[contains(., 'en-us')]]
選擇任何包含任何包含字符串en-us
屬性的元素:
HtmlDocument doc = new HtmlDocument();
doc.Load(path_to_html_file);
var nodes = doc.DocumentNode.SelectNodes("//*[@*[contains(., 'en-us')]]");
或者LINQ方式:
var nodes = doc.DocumentNode.Descendants()
.Where(n => n.Attributes.Any(a => a.Value.Contains("en-us")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.