Why This Error Happens
Error 6904 occurs when SQL Server’s XML parser encounters malformed XML data containing the same attribute name declared twice within a single element tag. This typically happens due to corrupted data during an INSERT/UPDATE operation, application-level string concatenation errors when building XML payloads, or encoding mismatches when importing XML from external sources like SSIS packages or linked servers. The underlying XML schema validation engine in SQL Server 2025 strictly enforces the W3C XML specification, which prohibits duplicate attributes within the same node.
Step-by-Step Resolution Guide
Step 1: Identify the Source of the Malformed XML
- Run the failing query in SSMS and capture the full error message, which includes the duplicate attribute name in the
%splaceholder. - Check the application layer (C#, Java, or Python connector) generating the XML string for improper concatenation logic, especially in loops that append attributes dynamically.
- Review SSIS package data flows or ETL scripts if the XML originates from
bcporBULK INSERToperations.
Step 2: Validate and Sanitize the XML Payload
- Extract the offending XML string using
SELECTand copy it into a validator tool like XML Notepad or an online XML linter to pinpoint the exact duplicate node. - Use
SELECT TRY_CAST(@xmlVariable AS XML)in a T-SQL script to isolate which row or batch is causing the parsing failure before the full transaction runs. - Modify the source application code to use parameterized XML builders (e.g.,
XmlWriterin .NET) instead of manual string concatenation, which prevents duplicate attribute injection entirely.
Step 3: Adjust Schema Validation and Retry the Operation
- If using an XML Schema Collection (XSD), open SQL Server Management Studio and navigate to Databases > [YourDB] > Programmability > Types > XML Schema Collections to ensure the schema definition doesn’t inadvertently allow duplicate keys.
- Re-run the
INSERT INTO TableName (XmlColumn) VALUES (@CleanedXml)statement with the sanitized data. - Restart the SQL Server (MSSQLSERVER) service via
services.mscon Windows Server 2025 only if the error persists due to a cached execution plan holding invalid schema metadata.
Alternative or Verification Steps
- Verify the Fix: Execute
SELECT * FROM sys.dm_exec_requests WHERE status = 'suspended'to confirm no XML parsing threads are hanging, then re-run the original query to ensure Error 6904 no longer appears in the SQL Server Error Log (Management > SQL Server Logsin SSMS). - Alternative Solution: If the error persists despite cleaning the XML, bypass strict validation temporarily by casting the data as
NVARCHAR(MAX)first, then useREPLACE()functions in T-SQL to strip duplicate attribute patterns via regex before casting back to theXMLdata type.