Troubleshooting Guide: SQL Server Error 9446 — “XML Parsing: Line %d”
Why This Error Happens
Error 9446 occurs when SQL Server’s XML parser encounters malformed, truncated, or incorrectly encoded XML data while attempting to parse a value into the xml data type. This is commonly caused by invalid characters embedded in the XML document, mismatched or unclosed tags, or data truncation that occurs when XML content exceeds buffer limits during insertion or import. It can also arise from corrupted data pages, improperly escaped special characters (such as &, <, >), or encoding mismatches (e.g., UTF-8 vs. UTF-16) between the source application and SQL Server 2025.
Step-by-Step Resolution Guide
Step 1: Identify the Exact Line and Source of the XML Error
- Run the failing query with
SET SHOWPLAN_XML OFFand capture the full error message, noting the specific line number referenced in the error. - Use
SELECT CAST(YourXmlColumn AS NVARCHAR(MAX))on the suspect row to visually inspect the raw XML content for malformed tags or special characters. - Check SQL Server Error Logs located at
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\ERRORLOGfor additional context around the failure timestamp.
Step 2: Validate and Sanitize the XML Input
- Open the source XML file or string in a validator (e.g., Visual Studio Code with an XML extension, or
xmllint.exefrom the Windows 11 / Windows Server 2025 developer tools) to confirm well-formed structure. - Ensure special characters are properly escaped: replace raw
&with&,<with<, and>with>in the source data. - If importing via SSIS or BCP, verify the
CodePageandDataFileTypesettings match the source encoding (UTF-8 vs UTF-16) to prevent truncation mid-parse.
Step 3: Check for Data Truncation in Transit
- Inspect the source application or ETL pipeline for
NVARCHAR(MAX)vsNVARCHAR(n)mismatches that could silently truncate long XML strings before they reach SQL Server. - If using a
.NETor ODBC driver connection string, confirmPacket Sizeis set sufficiently high (e.g.,Packet Size=32767) inODBC Data Source Administratoron Windows Server 2025. - Re-run the insert/import operation with a known-good, small XML sample to isolate whether the issue is data-specific or configuration-specific.
Step 4: Rebuild or Repair Corrupted XML Indexes
- If the error persists only on existing stored XML data (not new inserts), run
DBCC CHECKTABLE('YourTableName') WITH ALL_ERRORMSGSto check for underlying corruption. - Drop and rebuild any primary or secondary XML indexes using
DROP INDEXfollowed byCREATE PRIMARY XML INDEXto eliminate index-level corruption as the cause.
Alternative or Verification Steps
- Verify the fix by re-running the original failing query or import job and confirming Error 9446 no longer appears in
ERRORLOGor SQL Server Management Studio’s Messages pane. - As an alternative, stage the XML data as
NVARCHAR(MAX)first, run a validation check usingYourColumn.exist('/')to confirm well-formedness, and only then cast it to thexmldata type during final insertion.