Troubleshooting Guide: SQL Server Error 9403 – XML Parsing Failure
Why This Error Happens
Error 9403 occurs when SQL Server’s native XML parser encounters malformed, incomplete, or improperly encoded XML data during a cast, insert, or query operation against an xml data type column. This is typically caused by truncated data streams, mismatched tags, invalid character encoding (such as non-UTF-8 byte sequences), or XML documents that exceed the nested level limit. In SQL Server 2025 running on Windows Server 2025 or Windows 11, this can also surface after a database migration or ETL job where source data was not validated against a well-formed XML schema before insertion.
Step-by-Step Resolution Guide
Step 1: Identify the Exact Line and Source of the Malformed XML
- Review the full error message, which includes the specific line number (e.g., “XML parsing: line 12, unclosed tag”).
- Run the failing query in SQL Server Management Studio (SSMS) with
SET SHOWPLAN_XML OFFto isolate the exact statement. - Export the offending XML payload to a
.xmlfile and open it in a validator (e.g., Visual Studio orxmllint) to pinpoint the syntax break.
Step 2: Validate and Sanitize the XML Source Data
- Check for unescaped special characters (
&,<,>) that need to be converted to entities (&,<,>). - Confirm the encoding declaration matches the actual file encoding:
SELECT CAST(column_name AS xml) FROM table_name WHERE id = 1;
- If data is coming from an external ETL process or API, verify the source system is not truncating the payload mid-stream due to timeout or buffer limits.
Step 3: Adjust SQL Server Configuration and Retry the Operation
- Ensure the XML data does not exceed SQL Server’s default nested node limit (128 levels); restructure deeply nested XML if necessary.
- If using
OPENROWSET(BULK...)orOPENXML, confirm the file path is correct and accessible under the SQL Server service account permissions on Windows Server 2025:
C:\SQLData\ImportFiles\
- Re-run the import or query after correcting the XML structure:
DECLARE @x xml = '<Root><Item>Valid</Item></Root>';
SELECT @x;
Step 4: Check for Driver or Collation Mismatches
- Verify the client driver (ODBC, OLE DB, or ADO.NET) version is compatible with SQL Server 2025 — outdated drivers on Windows 11 clients can mangle XML payloads in transit.
- Confirm database collation supports the character set used in the XML (especially for non-Latin characters).
Alternative or Verification Steps
- Verify the fix by re-executing the original
CAST/CONVERTorINSERTstatement and confirming no error is returned; follow up withSELECT *.query('/')to confirm the XML parses and renders correctly. - If the error persists intermittently, use SQL Server Profiler or Extended Events to capture the raw XML payload at the moment of failure, as the issue may be an upstream application generating invalid XML under specific conditions (e.g., null concatenation or encoding mismatch).