Troubleshooting Guide: Error 6834 – Invalid XML Document in XMLTEXT Field
Why This Error Happens
Error 6834 occurs when SQL Server’s XML parser encounters data in an XMLTEXT field that does not conform to well-formed XML structure, typically due to a missing or malformed root element, unclosed tags, or invalid characters embedded in the document. This can happen when data is imported via BCP, BULK INSERT, or OPENROWSET from a source file that was truncated, encoded incorrectly (e.g., UTF-8 BOM issues), or generated by an application that produced non-compliant XML. It may also surface after a corrupted data page or an incomplete transaction left partial XML content in the field.
Step-by-Step Resolution Guide
Step 1: Identify the Offending Record
- Run a query isolating the column using
TRY_CASTorTRY_CONVERTto XML type to locate which row fails silently versus which throws the hard error. - Example:
SELECT ID, XMLColumn FROM YourTable WHERE TRY_CONVERT(xml, XMLColumn) IS NULL AND XMLColumn IS NOT NULL; - Export the flagged raw string value to a
.xmlfile for manual inspection in VS Code or Notepad++.
Step 2: Validate the Root Tag and Encoding
- Open the exported file and confirm there is exactly one root element wrapping all child nodes (XML requires a single parent node).
- Check for a Byte Order Mark (BOM) or invisible Unicode characters at the start of the file, which SQL Server’s parser rejects as invalid preamble content.
- Strip the BOM using PowerShell:
Get-Content -Path "C:\Temp\file.xml" -Encoding UTF8 | Set-Content -Path "C:\Temp\file_fixed.xml" -Encoding ASCII
Step 3: Re-Import or Repair the Data Source
- If the data originates from an ETL job or BULK INSERT on Windows Server 2025, verify the format file (
.fmt) correctly maps the field length so the XML isn’t truncated mid-tag. - Check the source application’s export logic for proper tag closure (e.g., ensure
<root>...</root>pairs match). - Update the application connection string to use
SQLXMLlibrary version compatible with SQL Server 2025 if legacy drivers are generating malformed payloads.
Step 4: Apply a Corrective Update Script
- If the root cause is a known malformed pattern (e.g., ampersands
¬ escaped as&), run aREPLACE()function within anUPDATEstatement to sanitize existing invalid characters before re-casting to XML. - Wrap the fix in a transaction to allow rollback if the
CASTstill fails:BEGIN TRAN; UPDATE YourTable SET XMLColumn = REPLACE(XMLColumn, '&', '&') WHERE ID = @BadID; SELECT CAST(XMLColumn as XML) FROM YourTable WHERE ID = @BadID;
Alternative or Verification Steps
- Verify the Fix: Re-run the original
INSERTorUPDATEstatement that triggered the 6834 error; if it processes without throwing an exception andSELECT ISNULL(XMLColumn, '').value('(/*/text())[1]', 'varchar(max)')returns expected data, the document is now well-formed. - Alternative Fix: If manual repair is impractical for bulk data, use
OPENROWSET(BULK...)with theSINGLE_CLOBoption to load the data as plain text first, allowing a C# or PowerShell pre-processing script to validate/correct the XML schema before final insertion into theXMLTEXTcolumn.