Why This Error Happens
Error 2323 occurs when SQL Server’s XML parser encounters a malformed or truncated XML document, typically because the closing tag does not correspond to the most recently opened tag. This is commonly caused by corrupted XML data being passed into functions like OPENXML, sp_xml_preparedocument, or XML-typed columns, often resulting from incomplete file transfers, manual editing errors, or encoding mismatches (such as UTF-8 vs UTF-16 BOM issues). In some cases, this also surfaces when SQL Server Configuration Manager or Policy-Based Management reads a corrupted .xml configuration file on Windows Server 2025 / Windows 11.
Step-by-Step Resolution Guide
Step 1: Identify the Source of the Malformed XML
- Check the application or T-SQL script that generated the XML payload (e.g.,
FOR XML,OPENXML, or an imported.xmlconfig file). - Use
SELECT CAST(YourXmlColumn AS XML)in SSMS to isolate the exact row or document causing the parse failure. - Note the line number referenced in the error message (
from line %u) to pinpoint the exact location in the file.
Step 2: Validate and Repair the XML Structure
- Open the offending XML file in a validating editor (Visual Studio Code with XML extension, or Notepad++ with XML Tools plugin).
- Look for unclosed tags, mismatched case sensitivity (XML is case-sensitive, e.g.,
<Data>vs</data>), or special characters that weren’t properly escaped (&,<,>must use&,<,>). - Confirm the file encoding matches what SQL Server expects — re-save as UTF-8 without BOM using:
notepad.exe file.xml
File > Save As > Encoding: UTF-8
Step 3: Clear Orphaned XML Document Handles (If Using OPENXML)
- If the error stems from
sp_xml_preparedocument, ensure every call has a matchingsp_xml_removedocumentto prevent memory corruption that can produce false parsing errors. - Example cleanup script:
EXEC sp_xml_removedocument @hDoc;
- Restart the SQL Server service via
services.mscorRestart-Service MSSQLSERVER(PowerShell, run as Administrator) to clear any cached document handles.
Step 4: Re-import or Re-run the Corrected XML
- Once the XML passes external validation, re-run the original
INSERT,UPDATE, or import job (SSIS package, bulk insert, or application call). - If using SQL Server Configuration Manager and the error points to a config file (e.g.,
SQLServerManager17.mscreading XML), replace the corrupted file with a known-good backup copy fromC:\Program Files\Microsoft SQL Server\160\Shared\.
Alternative or Verification Steps
- Verify the fix by running
SELECT CAST(@xmlVariable AS XML)or re-executing the import — if no error is thrown and the result set displays correctly, the XML is now well-formed. - As an alternative, use PowerShell’s built-in validator to pre-check files before passing them to SQL Server:
[xml](Get-Content "C:\path\to\file.xml")
If this command fails, PowerShell will report the exact line and character causing the mismatch, confirming the fix is needed before SQL Server is engaged again.