Troubleshooting Guide: SQL Server Error 9429 (XML Parsing Error)
Why This Error Happens
Error 9429 occurs when SQL Server’s internal XML parser encounters malformed, incomplete, or improperly encoded XML data at a specific line number within a column, variable, or document being processed. This typically stems from corrupted XML data being inserted into an xml typed column, mismatched or unclosed tags, invalid character encoding (such as non-UTF-8 byte sequences), or truncation of XML data during a network transfer or bulk insert operation. It can also surface when an application passes malformed XML strings into stored procedures that use OPENXML, sp_xml_preparedocument, or native XML methods like .nodes() or .query().
Step-by-Step Resolution Guide
Step 1: Identify the Exact Line and Source of the XML Data
- Note the exact line number referenced in the error message; this points to the specific location in the XML string causing the parse failure.
- Run a diagnostic query to extract the offending XML:
SELECT xml_column FROM your_table WHERE id = <problem_id>and inspect it in SQL Server Management Studio’s XML viewer. - Check the application or ETL log (in
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\on Windows Server 2025) for the raw payload sent prior to the error.
Step 2: Validate and Repair the XML Structure
- Use an external XML validator (such as
xmllintvia WSL on Windows 11, or Visual Studio’s XML editor) to confirm whether tags are improperly nested or unclosed. - Check for encoding mismatches — ensure the XML declaration (e.g.,
<?xml version="1.0" encoding="UTF-8"?>) matches the actual byte encoding of the data, especially if data originates from a BCP file or flat-file import. - If the issue stems from truncation, verify the
nvarchar(max)orxmlcolumn has not been cut off due to a buffer limit in the client driver (checkTDSpacket size settings in the connection string).
Step 3: Correct the Application/Import Process
- If using
OPENROWSET(BULK...)or BCP to load XML files, confirm theCODEPAGEandDATAFILETYPEparameters match the source file’s actual encoding. - For application-generated XML, review the serialization library (e.g., .NET’s
XmlWriter) to ensure special characters (&,<,>) are properly escaped before insertion. - Re-run the insert/update operation after correcting the payload:
DECLARE @x xml = N'<Root><Item>Test</Item></Root>';
INSERT INTO your_table (xml_column) VALUES (@x);
Step 4: Apply SQL Server Updates and Patches
- Confirm SQL Server 2025 is running the latest Cumulative Update via
SELECT @@VERSION;, as XML parser edge-case bugs are periodically patched by Microsoft. - Download and apply the latest CU from the Microsoft Update Catalog if running an older build.
Alternative or Verification Steps
- Verify the fix by re-executing the original query or stored procedure that triggered Error 9429 and confirming successful execution without exceptions.
- As an alternative, cast the XML data to
varchar(max)first usingTRY_CASTto isolate whether the issue is parser-related or data-related:SELECT TRY_CAST(xml_column AS xml) FROM your_table;— aNULLresult confirms malformed XML rather than a server-side bug.