Troubleshooting Guide: SQL Server Error 2271 – Duplicate Attribute
Why This Error Happens
Error 2271 occurs when SQL Server’s XML parser encounters the same attribute name declared more than once within a single XML element during parsing operations such as FOR XML, OPENXML, or XML schema validation. This typically results from malformed XML input data, a corrupted or hand-edited XML document, or application code that incorrectly constructs XML by appending duplicate attributes during serialization. It can also surface when importing XML configuration files or datasets that were generated by a third-party tool with a faulty XML export routine.
Step-by-Step Resolution Guide
Step 1: Identify the Offending XML Source
- Capture the exact XML payload being processed when the error fires, either from the application log, a SQL Server Profiler/Extended Events trace, or by logging the XML variable value just before the failing
OPENXML/sp_xml_preparedocumentcall. - Check the error message detail (
%ls) which names the specific duplicate attribute — search the XML for that attribute name. - Example query to isolate the issue:
DECLARE @xml XML = '<Root attr="1" attr="2"></Root>';
SELECT @xml;
Step 2: Validate and Correct the XML Document
- Open the XML in a validator (Visual Studio, VS Code with XML extension, or
xmllinton Windows via WSL) to confirm the duplicate attribute location. - Manually remove or merge the duplicate attribute values if hand-editing is feasible, ensuring only one instance of the attribute remains per element.
- If the XML is generated programmatically, inspect the source code (C#, .NET
XmlWriter, PowerShell, or ETL script) for logic that appends the same attribute twice, often due to a loop bug or incorrect dictionary/hash merge.
Step 3: Fix the Upstream Data Generation Process
- If using SSIS, Integration Services packages, or a custom ETL job on Windows Server 2025, review the XML mapping/transformation step for duplicate column-to-attribute bindings.
- Update any
FOR XML AUTO/RAW/PATHqueries to ensure column aliases used as attribute names are unique, e.g.:
SELECT ID AS '@ID', Name AS '@Name'
FROM dbo.MyTable
FOR XML PATH('Record')
- Redeploy the corrected package or script and rerun the import/export job that originally triggered Error 2271.
Step 4: Patch and Restart Services
- Ensure SQL Server 2025 is fully patched via Windows Update or SQL Server Cumulative Update channel, since XML parser edge-case bugs are occasionally addressed in CU releases.
- Restart the SQL Server service (
net stop MSSQLSERVERthennet start MSSQLSERVER) after applying updates to ensure the XML parsing engine reloads cleanly.
Alternative or Verification Steps
- Re-run the original query or import job with the corrected XML and confirm Error 2271 no longer appears in
SQL Server Error Logor Windows Event Viewer (Application log, Source: MSSQLSERVER). - As an alternative, wrap the XML ingestion logic in a validation step using
sys.dm_exec_describe_first_result_setor a pre-check withTRY_CAST(@xml AS XML)combined with custom attribute de-duplication logic before passing data into SQL Server.