Using Htmlagility pack getting attribute values

c# html-agility-pack html-parsing

Question

Using Htmlagilitypack I may use the following code to get the attribute value for a single tag:

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+"'" ;
     }
 }

I need to retrieve all of those text type values as output, however when it comes to the first value, it stops working and returns that value.

What must I do to achieve this?

1
0
7/27/2013 7:45:49 AM

Accepted Answer

You mustSelectNodes in place ofSelectSingleNode

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

if input type and value are required

var inputs = doc.DocumentNode.SelectNodes("//input").Select(n => new { 
                 Type = n.Attributes["type"].Value, Value = n.Attributes["value"].Value }).ToList();
1
7/27/2013 7:40:06 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow