Using LINQ to exctract all hidden inputs from an HTML using C# -
hello have following html 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 extract names , values input type hidden using c# linq or string functios?
using htmlagilitypack, can following:
var doc = new htmlweb().load("http://www.mywebsite.com"); var nodes = doc.documentnode.selectnodes("//input[@type='hidden' , @name , @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 want load document text file instead of url, can do:
var doc = new htmldocument(); doc.load(@"c:\file.html");
if still want use linq purpose, selectnodes
returns htmlnodecollection
ienumerable<node>
, can do:
var query = f in doc.documentnode.descendantnodes() 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); }
Comments
Post a Comment