---
title: "Read HTML form input values in C#"
url: https://recursiveai.org/onehtml/cookbook/read-html-form-field-values/
description: "Parse static HTML form fields in C#: read named inputs, checked checkboxes, selected options and textarea text with the OneHTML DOM."
version: "1.6.0"
---

# Read HTML form input values in C#

**onehtml** is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneHtml) · [GitHub](https://github.com/recursive-ai-oss/onehtml-csharp) · [Docs](https://recursiveai.org/onehtml/docs/) · [Download example (.zip)](https://recursiveai.org/onehtml/cookbook/read-html-form-field-values/read-html-form-field-values.zip)

Read static form markup, not live browser values or submitted FormData.

```shell
dotnet add package RecursiveAI.OneHtml
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneHtml;

var doc = Document.Parse("""
    <input name="user" value="Ada">
    <input type="checkbox" name="news"
      checked value="yes">
    <input type="checkbox" name="alerts" value="no">
    <input type="radio" name="choice" value="no">
    <select>
      <option selected>Pro</option>
    </select>
    """);
foreach (var field in
    doc.Root.QuerySelectorAll(
    "input[name]:not([type=checkbox]):not([type=radio]), " +
    "input[checked], " +
    "option[selected]"))
    Console.WriteLine(
        field.GetAttribute("value")
        ?? field.TextContent);
```

```text
Ada
yes
Pro
```

Unzip [the example](https://recursiveai.org/onehtml/cookbook/read-html-form-field-values/read-html-form-field-values.zip), then `dotnet run`.

## The full program

The full program scopes its selectors to one form and skips unchecked checkboxes and radio buttons. The HTML is embedded in `Program.cs`, so no input file is needed.

```csharp
using RecursiveAI.OneHtml;

var doc = Document.Parse("""
    <form id="profile">
      <input name="user" value="Ada &amp; Co">
      <input type="checkbox" name="news" value="yes" checked>
      <input type="checkbox" name="alerts" value="yes">
      <select name="plan">
        <option value="free">Free</option>
        <option value="pro" selected>Pro</option>
      </select>
      <textarea name="note">Hello</textarea>
    </form>
    """);
foreach (var input in doc.Root.QuerySelectorAll("#profile input[name]"))
{
    var type = input.GetAttribute("type") ?? "text";
    if ((type == "checkbox" || type == "radio") &&
        input.GetAttribute("checked") is null) continue;
    Console.WriteLine($"{input.GetAttribute("name")}={input.GetAttribute("value") ?? (type is "checkbox" or "radio" ? "on" : "")}");
}
foreach (var select in doc.Root.QuerySelectorAll("#profile select[name]"))
    foreach (var option in select.QuerySelectorAll("option[selected]"))
        Console.WriteLine($"{select.GetAttribute("name")}={option.GetAttribute("value") ?? option.TextContent}");
foreach (var text in doc.Root.QuerySelectorAll("#profile textarea[name]"))
    Console.WriteLine($"{text.GetAttribute("name")}={text.TextContent}");
```

`dotnet run` prints:

```text
user=Ada & Co
news=yes
plan=pro
note=Hello
```

A missing attribute returns `null`; the program uses explicit fallbacks instead of assuming every control has a value.

## Variations

**Inspect both checked and unchecked boxes.** Add this after the full program:

```csharp
foreach (var checkbox in doc.Root.QuerySelectorAll(
    "input[type=checkbox][name]"))
    Console.WriteLine($"{checkbox.GetAttribute("name")}: " +
        (checkbox.GetAttribute("checked") is not null));
```

```text
news: True
alerts: False
```

## Related recipes

- [Parse HTML](https://recursiveai.org/onehtml/cookbook/parse-html/)
- [Use CSS selectors](https://recursiveai.org/onehtml/cookbook/css-selectors/)
- [Read an HTML table](https://recursiveai.org/onehtml/cookbook/parse-html-table/)

## The short answer

Parse the HTML with `Document.Parse`, find controls with `QuerySelectorAll`, and read attributes with `GetAttribute`. For text inputs, the stored value is the `value` attribute. For a textarea, read `TextContent`. For a select list, find the options explicitly marked `selected` and use each option’s `value`, falling back to its text when no value is present.

This recipe reads the document you give it. It does not open a browser, run a page’s scripts or recover values a user typed after the page loaded. That distinction matters when you are inspecting a saved page or a server response: the response can contain an empty form even though the live browser later displays filled-in fields.

## How do I parse HTML in C#?

Call `Document.Parse` with the HTML string, then query the parsed tree through `doc.Root` using OneHTML’s [DOM API](https://recursiveai.org/onehtml/docs/). A selector such as `#profile input[name]` narrows the result to named input elements inside a particular form. Scoping matters on pages with several forms, where the same field name can appear in more than one place. The full program embeds all of its input so that you can see exactly which controls it reads.

The sample returns decoded text: `Ada &amp; Co` in the markup becomes `Ada & Co` in the output. Read text and attributes from the DOM instead of slicing raw HTML around quote characters. Attribute order, omitted attributes and character references otherwise make seemingly simple string searches fragile.

Keep repeated field names as repeated entries when they matter to your application. A dictionary that overwrites an earlier value is the wrong representation for a group of checkboxes or a multiple-selection list. The loops here print individual pairs and never silently collapse them into a single value. If you store them, use a list of pairs or a mapping from names to lists.

## How do I get an HTML input value?

For ordinary named inputs, call `GetAttribute("value")`. A checkbox or radio button needs another decision: is its `checked` attribute present? The full program skips unchecked controls; the variation instead prints their markup state. A boolean attribute’s presence matters, not a comparison of its contents with the word “true”. The sample’s checked attribute has no assigned text at all.

The code provides `"on"` when a checkbox or radio value is missing, and an empty string for other inputs. If your task is an exact attribute inventory rather than form-like extraction, preserve `null` instead. Select the fallback according to the control type and your intended output.

Selected options and textarea content live in different places from input values. The full program looks for `option[selected]` inside each named select and reads textarea text separately. A select with no explicit selected attribute produces no option in this extraction, even if a browser would choose a default. Add a deliberate default-selection rule only if your task needs one.

These loops are not the browser’s [form submission algorithm](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-entry-list). They do not account for disabled controls, submitter buttons, controls associated with a form by an external `form` attribute, or the browser’s current edited state. Hidden fields are still inputs and can be useful for inspection, but reading their values does not make a valid authenticated submission. For the actual submitted payload, capture it in the running browser or implement the submission rules your application requires. Use this recipe when the question is what the static markup contains.
