Troubleshooting Guide: SQL Server Error 2396 — “%sAttribute may not appear outside of an element”
Why This Error Happens
This error occurs when SQL Server 2025 attempts to parse a malformed XML document or an XML-based configuration file (such as an SSIS package, Policy-Based Management policy, or an XML column payload) where an attribute is defined outside the scope of a valid parent element. This is typically caused by corrupted XML metadata, a manually edited .dtsx or .xml file with broken tag nesting, or truncated data being passed into an XML typed column via OPENROWSET or sp_xml_preparedocument. In some cases, this also surfaces when Windows Server 2025 security policies interfere with how the MSXML parser (used internally by the Database Engine) reads XML streams during deserialization.
Step-by-Step Resolution Guide
Step 1: Validate the Source XML Document
- Open the offending XML file (configuration file, SSIS package, or XML variable source) in a validating editor like Visual Studio 2025 or XML Notepad.
- Look specifically for attributes placed after a closing tag (
</element> attribute="value") or attributes floating at the root level without a parent element. - Run a quick syntax check using PowerShell:
[xml](Get-Content "C:\Path\To\File.xml")— if this throws an error, the file is confirmed malformed.
Step 2: Repair or Re-Export the Corrupted Metadata
- If the error originates from an SSIS package, re-deploy the package from the original Visual Studio project rather than a manually patched
.dtsxfile. - If the issue is tied to a Policy-Based Management (PBM) policy, navigate to
Management > Policy Management > Policiesin SSMS, right-click the suspect policy, and select Export Policy to regenerate clean XML. - For data being inserted into an
XMLcolumn, verify the source application isn’t truncating the payload mid-stream; check buffer limits in your ETL or API layer.
Step 3: Clear Corrupted Cache and Restart Services
- On Windows Server 2025, navigate to
C:\Program Files\Microsoft SQL Server\MSSQL17.MSSQLSERVER\MSSQL\DATAand check for orphaned temp XML schema cache files. - Restart the SQL Server service via PowerShell:
Restart-Service -Name 'MSSQLSERVER' -Force - Restart SQL Server Agent as well if the error was triggered by a scheduled job:
Restart-Service -Name 'SQLSERVERAGENT' -Force
Step 4: Rebuild the XML Schema Collection (If Applicable)
- If a custom XML Schema Collection (XSD) is tied to the column, drop and recreate it:
DROP XML SCHEMA COLLECTION dbo.YourSchemaName;- Re-import the corrected, validated schema using
CREATE XML SCHEMA COLLECTION.
Alternative or Verification Steps
- To verify the fix, re-run the original operation (SSIS package execution, policy evaluation, or query) and confirm Error 2396 no longer appears in the SQL Server Error Log (
Management > SQL Server Logsin SSMS). - As an alternative, if re-validating XML is not feasible, isolate the problematic record or file and process it through an external XML linter (like
xmllint --noout file.xmlon a WSL instance) before reintroducing it to SQL Server.