Troubleshooting Guide: SQL Server Error 6601 – XML Parse Error
Why This Error Happens
Error 6601 occurs when SQL Server’s internal XML parser encounters malformed, incomplete, or improperly encoded XML data while attempting to parse a value into the xml data type. This is commonly caused by corrupted XML data being inserted from an external source, mismatched or unclosed tags, invalid characters, or encoding mismatches (such as UTF-8 vs UTF-16 conflicts) between the source file and SQL Server’s expected format. It can also surface during upgrades or data imports where legacy XML columns contain data that no longer conforms to strict XML schema validation in SQL Server 2025.
Step-by-Step Resolution Guide
Step 1: Identify the Exact Line and Character Causing the Failure
- Note the line number (%d) and hex error code (0x%x) provided in the error message — these map directly to the offending XML fragment.
- Run the failing query in SQL Server Management Studio (SSMS) with
SET SHOWPLAN_XML OFFand isolate the specificINSERT,UPDATE, orCAST/CONVERTstatement causing the issue. - Extract the raw XML string using
SELECTand inspect it in a text editor like VS Code or Notepad++ with “Show All Characters” enabled to detect hidden control characters.
Step 2: Validate and Repair the XML Source Data
- Check for unclosed tags, mismatched namespaces, or invalid entity references (e.g., unescaped
&,<,>) in the source document. - If the XML originates from an external file or application, re-export or regenerate it to ensure it conforms to well-formed XML 1.0 standards.
- Use
SELECT CAST(@xmlVariable AS XML)on a sample string in isolation to confirm whether the parser throws the same 0x% error code outside the full batch.
Step 3: Correct Encoding Mismatches
- Confirm the source file encoding matches what SQL Server expects (UTF-8 without BOM is recommended for
OPENROWSET(BULK...)imports). - In Windows Server 2025 / Windows 11, open the file in Notepad and use “Save As” to explicitly set encoding to “UTF-8” before re-importing.
- If importing via SSIS or BCP, verify the
-Ccodepage flag or SSIS Flat File connection manager’s “Code Page” property matches the source encoding.
Step 4: Apply SQL Server Cumulative Updates
- Navigate to Microsoft’s official download center and confirm you are running the latest Cumulative Update (CU) for SQL Server 2025, as XML parser bug fixes are frequently included.
- Check the installed build number via
SELECT @@VERSION;in a new query window. - Apply the update through SQL Server Setup, selecting “Product Updates” during the maintenance wizard on Windows Server 2025.
Step 5: Rebuild or Re-import the Affected XML Column Data
- If the corruption persists in a stored table, export the affected rows to a staging table using
SELECT * INTO StagingTable FROM OriginalTable WHERE XMLColumn IS NOT NULL. - Sanitize and re-validate each XML entry individually before re-inserting into the production table.
Alternative or Verification Steps
- After applying fixes, re-run the original query or import job and confirm no 6601 error is thrown; validate success by running
SELECT XMLColumn.exist('/')on affected rows to confirm valid XML structure. - If the error persists, bypass strict parsing by loading the data as
NVARCHAR(MAX)first, then useTRY_CAST(... AS XML)to isolate and quarantine only the specific malformed rows without failing the entire batch.