Search code examples
c#.nethtml-agility-packselectsinglenode

How can I get a specific attribute value?


There is an input element as below:

<input type="hidden" value="908" name="a">

I want to return 908 value using DocumentNode.SelectSingleNode. How can I do this?
The code below does not work?

string a = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='a']").GetAttributeValue("value", true).ToString();

Solution

  • You're calling the wrong overload for GetAttributeValue(). Try using a string as the second parameter instead of a boolean:

    string a = htmlDoc.DocumentNode.SelectSingleNode("//input[@name='a']").GetAttributeValue("value", "default");