ErrLookup › Background articles › InvalidDataException: when .NET data is malformed, truncated, or in the wrong format

InvalidDataException: when .NET data is malformed, truncated, or in the wrong format

InvalidDataException is a System.IO exception that .NET libraries throw when the content of a file, stream, or payload does not match the format the parser expected. Developers meet it when JSON will not deserialize, an XML document has no root element, a binary header's magic bytes are wrong, a cached artifact is from an incompatible version, or a downloaded file changed mid-transfer. Unlike IOException (which blames the channel) or ArgumentException (which blames the caller), InvalidDataException blames the data itself, so the fix is almost always to repair, regenerate, or replace the offending input rather than to retry the call.

Distilled from 278 documented records across 26 repositories.

Background

InvalidDataException lives in the System.IO namespace and was introduced to signal that a data stream or file is in an unexpected, malformed, or unrecognizable format. It sits below the format-specific exceptions a library might also define. The distinguishing intent is the blame target: IOException faults the transport or disk, ArgumentException faults the calling code, but InvalidDataException faults the content of the bytes or text that were read. In every one of the documented records the throw site is hand-written library code that has just parsed input and found it inconsistent with an expected schema or invariant.,The layer that produces it is application-level parsing, deserialization, and format reading, not the runtime or the base class library itself. A library reads bytes or text from a file or network response, validates them against a structural contract, and throws when the contract is violated. The records show this gate operating on serialized text formats (JSON arrays containing null elements, XML documents with no root element, CSV rows shorter than their header), on binary headers (a VOC file whose description does not begin with 'Creative Voice File', a DependencyGraph whose magic signature does not match), on domain-specific configuration DSLs (OpenRA BooleanExpression tokens where a number is glued to an identifier), and on integrity metadata (a resumed download whose ETag changed between requests).,The family splits into several recognizable flavors. The largest group is structural validators that fire eagerly at load time: a file that parsed syntactically but is semantically empty or incomplete (an empty settings file, an XML snippet with no document element, a config whose root element is not the expected name). A second group is format-gate rejections, where a parser encounters a type tag, version byte, or terrain-type index it has no case for (.DS_Store record types, WAV format tags, tileset terrain bytes, DependencyGraph version numbers). A third group is integrity and restart guards, where an engine detects that a download or cached artifact can no longer be trusted (a changed ETag on resume, a bad Content-Range after a retry) and refuses to continue. A fourth, smaller group is defensive invariant guards on hardcoded constants (the OpenRA Huffman table and VOC frequency-divisor checks) that are effectively unreachable through normal input and surface instead as build or type-initialization defects.,From the caller's side the exception is synchronous, often fatal to the current operation, and carries a message that names the offending value and its source location. Because the blame is on content, the recovery direction is consistent across libraries: fix or replace the data, not retry it unchanged. The records repeatedly direct users to regenerate the file from a canonical source, restore a clean copy, delete a stale generated cache and rebuild, or convert the input to a standard supported format. Libraries that handle the exception robustly wrap their loaders in try/catch with a documented fallback (skip the malformed entry, fall back to a default theme, try the next format loader) rather than letting one bad record abort the entire operation.

Common causes

What usually fixes it

Go deeper

Documented occurrences

…and 258 more across the corpus — use search.

Honest provenance: generated on 2026-08-13 from AI-assisted analysis of the linked records. See how records are made.