0

使用Htmlagilitypack我可以使用以下代码获取一个标签的属性值:

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

当它获得第一个值时,它会停止执行并给出该值,但我需要将所有这些文本类型值作为输出。

为此,我需要做什么?

4

1 回答 1

1

你需要SelectNodes而不是SelectSingleNode

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

如果您需要输入类型和值

var inputs = doc.DocumentNode.SelectNodes("//input").Select(n => new { 
                 Type = n.Attributes["type"].Value, Value = n.Attributes["value"].Value }).ToList();
于 2013-07-27T06:33:47.533 回答