AAP AAP - 2 months ago 7
C# Question

HTMLAgilityPack How to parse a specific attribute in HTML Contents

Can anyone please help me how to parse a specific attribute (if I understand right). the below is a sample.



<div class="buy-container">
<form action="/content/action" method="post">
<button class="product-buy">buy</button>
<input type="hidden" name="ActionAddToBasket">
<input type="hidden" value="198300" name="ContentNodeID">
<input type="hidden" value="778768" name="ContentObjectID">
<input type="hidden" value="full" name="ViewMode">
</form>
</div>




I would like to parse where value = "778768" from the above html data.
Please advice. Many thanks.

Answer

You could do something like this.

  var val = doc
              .DocumentNode
              .SelectSingleNode("//input[@type='hidden' and @name='ContentObjectID']")
              .Attributes["value"].Value;

Check this fiddle demo

Comments