3

Hello i have the following HTML from a webclient code

<body onLoad="window.focus()">
<form name="form1" method="post" action="/www/www.do">
<input type="hidden" name="value1" value="aaaa">
<input type="hidden" name="value2" value="bbbb">
<input type="hidden" name="value3" value="cccc">
<input type="hidden" name="value4" value="dddd">
<input type="hidden" name="value5" value="eeee">

more html.....

</body>

How can i extract all names and values that are input type hidden using C# linq or string functios?

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
Joe Dixon
  • 1,557
  • 2
  • 10
  • 14

1 Answers1

2

Using HtmlAgilityPack, you can do the following:

var doc = new HtmlWeb().Load("http://www.mywebsite.com");
var nodes = doc.DocumentNode.SelectNodes("//input[@type='hidden' and @name and @value]");
foreach (var node in nodes) {
    var inputName = node.Attributes["name"].Value;
    var inputValue = node.Attributes["value"].Value;
    Console.WriteLine("Name: {0}, Value: {1}", inputName, inputValue);
}

If you want to load the document from a text file instead of from a URL, you can do:

var doc = new HtmlDocument();
doc.Load(@"C:\file.html");

If you still want to use LINQ for that purpose, as SelectNodes returns an HtmlNodeCollection which is an IEnumerable<Node>, you can do:

var query = from f in doc.DocumentNode.DescendantNodes()
            where f.Name == "input" && f.GetAttributeValue("type", "") != ""
                    && f.Attributes.Contains("name") && f.Attributes.Contains("value")
            select new
                        {
                            f.Attributes["value"].Value,
                            f.Attributes["name"].Name
                        };

foreach (var q in query) {
    Console.WriteLine("Name: {0}, Value: {1}", q.Name, q.Value);
}
Oscar Mederos
  • 29,016
  • 22
  • 84
  • 124