yasmuru
yasmuru

Reputation: 1186

Using Htmlagility pack getting attribute values

Using Htmlagilitypack I can get the attribute value for one tag by using following code:

public string parseinput(HtmlDocument HtmlDocument)
{
     try
     {
            return HtmlDocument.DocumentNode.SelectSingleNode("//input[@type=""text""]").Attributes["value"].Value;
     }

     catch (Exception ex)
     {
          string x= ex.ToString();
            return "Error is... '"+x+"'" ;
     }
 }

When it gets first value, it stops executing and gives that value, but I need to get all those text type values as output.

For this what do I need to do?

Upvotes: 0

Views: 1972

Answers (1)

Damith
Damith

Reputation: 63065

you need SelectNodes instead of SelectSingleNode

return String.Join(",", HtmlDocument.DocumentNode.SelectNodes("//input[@type=""text""]")
                         .Select(n=>n.Attributes["value"].Value)

If you need both input type and value

var inputs = doc.DocumentNode.SelectNodes("//input").Select(n => new { 
                 Type = n.Attributes["type"].Value, Value = n.Attributes["value"].Value }).ToList();

Upvotes: 1

Related Questions