0

Hi anyone know any good C# library that can select a form inside a string containing HTML

<html>
<body>
some text or layout
..
<form id="blabla" name="blabla" method="post" action="register.aspx">
<input type="hidden" name="token" value="12345">
<input type="text" name="username" value="">
<input type="text" name="password" value="">
</form>
..
some text or layout
</body>

string HTML = (above)
FormObject formtag = GetForm(HTML); // get only the <form>..</form>
var a = formtag.Method // get or post
var b = formtag.Action // get the URL it post

// construct the post string
// username={0}&password={1}&token=12345
var poststring = string.Format(GetPostString(formtag), "Anima", "abcdef");
// result
// username=Anima&password=abcdef&token=12345

Is there any C# library that can get form and all the input items inside the form?

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364

1 Answers1

1

You may take a look at the HTML Agility Pack library which allows you to parse HTML:

var htmlDoc = new HtmlAgilityPack.HtmlDocument();
htmlDoc.LoadHtml(html);
var form = htmlDoc.DocumentNode.SelectSingleNode("//form[@id='blabla']");
if (form != null)
{
    ...
}
Patman
  • 41
  • 8
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928