System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.: what it means and how to fix it

TL;DR: Validate locally, fix the first real error, validate again (no upload).

Troubleshoot System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. and fix invalid XML locally (no upload).

What the error means

System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. means the XML parser found invalid markup. Most XML issues are fixable by correcting the first broken tag/character and re-validating.

Most common real-world causes

  • Mismatched tags (opening/closing tags do not match).
  • Unescaped reserved characters (especially & in text).
  • Multiple root elements (XML must have exactly one root).
  • Invalid characters (control chars, bad Unicode) inside the file.
  • Wrong encoding or a stray BOM/prolog mismatch.
  • Cut/truncated XML (missing closing tags).

Fast debugging steps

  • Validate the first error first. Fixing the first broken tag usually resolves many downstream errors.
  • Search around the reported line/column for mismatched tags or a raw '&' that should be '&'.
  • Confirm the XML starts with a single root and ends with the matching closing tag.
  • If the file comes from a feed/export, try a smaller sample to isolate the first invalid record.

Code example (csharp)

using System;
using System.Xml;

try {
  var doc = new XmlDocument();
  doc.LoadXml(xmlText);
  Console.WriteLine(doc.DocumentElement?.Name);
} catch (XmlException e) {
  Console.WriteLine(e.Message);
  Console.WriteLine($"Line={e.LineNumber} Position={e.LinePosition}");
}

Fix without uploading data

If the XML contains sensitive data, validate and convert locally. No Upload Tools runs 100% in your browser.

  • XML to JSON to trigger strict parsing and see the first parser error.
  • JSON Validator to validate output if you post-process to JSON.

Workflow: validate -> fix the first error -> validate again -> export/convert.

FAQ

Why do I get different errors in different tools? XML parsers differ. Always fix the first error in the raw input and re-check.

Should I regex-fix XML? Avoid blind regex edits. Validate after each change so you know what you fixed.

Privacy & Security
All processing happens locally in your browser. Files are never uploaded.

Quick fix checklist

  • Reproduce the error on a minimal input.
  • Check type/format and field mapping.
  • Apply the smallest safe fix.
  • Validate on production-like payload.

Next pages to check

Closest crawled pages without impressions yet. Added to speed first-impression conversion.

neighbor csharp csharp newtonsoft error converting 123 system int32 user id workflows daneighbor csharp csharp newtonsoft error converting 2026 02 17 system boolean id workflowneighbor csharp csharp newtonsoft error converting false system datetime value workflowsneighbor csharp csharp newtonsoft error converting nan system datetime id workflows dataneighbor csharp csharp newtonsoft error converting null system boolean user id checklistneighbor csharp csharp newtonsoft error converting null system collections generic list neighbor csharp csharp newtonsoft error converting null system datetime value checklistsneighbor csharp csharp newtonsoft error converting null system datetime value troubleshoneighbor csharp csharp newtonsoft error converting null system decimal items 0 id checklneighbor csharp csharp newtonsoft error converting null system double id checklists dataneighbor csharp csharp newtonsoft error converting null system net ipaddress user id worneighbor csharp csharp newtonsoft error converting null system timespan id runbooks dataneighbor csharp csharp newtonsoft error converting null system timespan id workflows datneighbor csharp csharp newtonsoft error converting null system timespan items 0 id runboneighbor csharp csharp newtonsoft error converting null system timespan items 0 id workfneighbor csharp csharp newtonsoft error converting true system boolean value checklists neighbor csharp csharp newtonsoft error converting true system boolean value workflows dneighbor csharp csharp newtonsoft error converting true system decimal user id workflows