Skip to content

Fix XmlDeserializer to correctly handle nested XML elements with duplicate tag names  #2340

@qodo-free-for-open-source-projects

Description

Description

  • XmlDeserializer incorrectly flattens nested lists by using Descendants() which includes elements at all depths, causing deeply nested items to be included in parent collections
  • RootElement selection fails when XML contains duplicate element names at different nesting levels, throwing InvalidOperationException or selecting wrong element
  • RemoveNamespace() processing includes null values causing potential issues, and unnecessary null-forgiving operators reduce code safety

Deliverables

  • Fix list handling: Modify HandleListDerivative to detect container elements and use Elements() for direct children only, with Descendants() as fallback for backward compatibility
  • Fix RootElement: Implement shallowest-match preference by trying Element() first, then DescendantsAndSelf() ordered by ancestor count
  • Improve robustness: Filter null values in RemoveNamespace, remove null-forgiving operators, add validation for generic type names in XML element names
  • Add test coverage: Create comprehensive test suite covering nested lists, duplicate element names, and complex nesting scenarios

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions