Troubleshooting Guide: SQL Server Error 9420 — “XML Parsing: Line %d”
Why This Error Happens
Error 9420 occurs when SQL Server’s internal XML parser encounters malformed, incomplete, or improperly encoded XML data being inserted into an XML data type column, passed to an XML method (like .nodes(), .value(), or .query()), or processed through FOR XML/OPENXML operations. This typically results from a missing closing tag, an invalid character encoding (such as a BOM mismatch or illegal Unicode character), a broken namespace declaration, or truncated data caused by network interruption or a buffer/datatype length limit during data transfer. It can also surface in Windows Server 2025 / Windows 11 environments when application-layer serialization (e.g., .NET, PowerShell, or ETL tools) generates XML with inconsistent encoding declarations that SQL Server 2025’s stricter XML engine rejects.
Step-by-Step Resolution Guide
Step 1: Identify the Exact Line and Source of the Malformed XML
- Capture the full error message — it specifies the line number (and often column) where parsing failed.
- Extract the raw XML payload using
SELECT CAST(YourXmlColumn AS NVARCHAR(MAX))or by logging the source string before insertion. - Run the extracted string through a validator (e.g.,
SELECT CAST(@xmlString AS XML)in a test query) to reproduce the error in isolation.
Step 2: Validate and Repair the XML Structure
- Check for unclosed tags, mismatched case-sensitive element names, or invalid special characters (
&,<,>not properly escaped as&,<,>). - Verify the XML declaration (
<?xml version="1.0" encoding="UTF-8"?>) matches the actual encoding of the byte stream — mismatches commonly trigger 9420 in SQL Server 2025. - If the XML is generated by an external application, inspect serialization settings (e.g., .NET
XmlWriterSettings.Encoding) to ensure UTF-8/UTF-16 consistency.
Step 3: Check for Data Truncation or Buffer Limits
- Confirm the source column or variable isn’t truncating the XML string — use
NVARCHAR(MAX)instead of fixed-length types when staging XML before casting. - Review any network-based data transfer (linked servers, SSIS packages, REST API calls) for timeout or packet-size issues that could cut off the XML mid-stream.
- Increase
MAX TEXT REPL SIZEor adjust application connection string parameters (e.g.,Packet Sizein SSMS/ODBC) if large XML documents are involved.
Step 4: Update SQL Server 2025 and Validate Compatibility Mode
- Apply the latest Cumulative Update (CU) for SQL Server 2025 via SQL Server Setup or
Microsoft Update, since early builds have had stricter XML schema validation that flags previously tolerated XML. - Check the database compatibility level with
SELECT compatibility_level FROM sys.databases WHERE name = 'YourDB';and test against the XML parser behavior if recently upgraded from SQL Server 2019/2022. - Restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVER(PowerShell, run as Administrator) after applying updates.
Alternative or Verification Steps
- Verify the fix: Re-run the original INSERT/UPDATE/query operation that failed and confirm no 9420 error is raised; additionally, validate the XML with
SELECT CAST(@FixedXml AS XML);to ensure it parses cleanly. - Alternative workaround: If the XML source cannot be immediately corrected, store the data temporarily as
NVARCHAR(MAX)instead ofXMLtype, then sanitize/correct it via a scheduled script before casting toXMLfor long-term storage.