Why This Error Happens
Error 2350 occurs when SQL Server’s XML parser encounters malformed or unexpected content while processing an XML document, schema collection, or configuration fileāmost commonly triggered by invalid characters, improperly escaped entities, or a mismatch between the XML data and its associated XSD schema. This frequently happens after importing XML data from external sources (APIs, flat files, or third-party applications) that don’t conform to well-formed XML standards. It can also surface when SQL Server Management Objects (SMO) or Policy-Based Management files have been corrupted or manually edited incorrectly.
Step-by-Step Resolution Guide
Step 1: Identify the Source of the Invalid XML
- Run the failing query or import process in SQL Server Management Studio (SSMS) with
SET FMTONLY OFFto capture the exact XML payload. - Use
SELECT CAST(your_xml_column AS XML)on a copy of the data in a test table to isolate which row or element triggers the error. - Check for unescaped special characters (
&,<,>) that haven’t been converted to entities (&,<,>).
Step 2: Validate Against the XML Schema Collection
- If using a typed XML column, run
SELECT * FROM sys.xml_schema_collectionsto confirm the correct schema is bound. - Validate the XML manually against the schema using PowerShell:
[System.Xml.Schema.XmlSchema]::Read(...)
- Look for missing namespaces, incorrect element ordering, or unclosed tags that violate the schema definition.
Step 3: Repair or Re-Import Clean Data
- Strip or sanitize invalid characters at the source (e.g., using
REPLACE()or a pre-processing script) before inserting into SQL Server. - If importing via SSIS, open the package in Visual Studio and inspect the XML Source component’s error output path for the exact byte offset causing the failure.
- Re-run the import after correcting the source file using a validated XML editor (e.g., Visual Studio Code with XML extension).
Step 4: Check for Corrupted Configuration Files (if error is server-side)
- Navigate to
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\Log\and inspect the ERRORLOG for the full stack trace. - If the error relates to Policy-Based Management or Maintenance Plans, verify the XML stored in
msdb.dbo.sysxmitqueueor policy definition tables isn’t corrupted. - Restore the affected configuration from a known-good backup if corruption is confirmed.
Alternative or Verification Steps
- Re-run the original query or import job and confirm no Error 2350 appears in SSMS or the Windows Event Viewer under Applications and Services Logs > MSSQLSERVER.
- As an alternative, use
sp_xml_preparedocumentwith explicit error handling in aTRY...CATCHblock to gracefully log and skip malformed XML rows instead of halting the entire batch.