Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 1, 2025

Fixed an issue where empty XML elements were not properly deserialized when using XmlElement properties, causing subsequent elements to be incorrectly consumed or skipped.

Problem

When deserializing XML with empty elements followed by other elements, the XmlElement property would return null for empty elements, potentially causing misalignment in the parsing of subsequent elements. For example:

public class Root
{
    public XmlElement Description { get; set; }
    public string Name { get; set; }
}

var serializer = new XmlSerializer(typeof(Root));

// This would incorrectly return Description = null instead of an empty XmlElement
var result = serializer.Deserialize(new StringReader(@"<root><description></description><name>Test</name></root>"));

Root Cause

The ReadXmlNode method in XmlSerializationReader was returning null for empty XML elements instead of creating an appropriate empty XmlElement node. This occurred for both self-closing elements (<description/>) and empty elements with explicit tags (<description></description>).

Solution

Modified the ReadXmlNode method to:

  1. Detect when an element is empty (either self-closing or with no content between tags)
  2. Create an empty XmlElement with the correct local name and namespace
  3. Preserve any attributes that were present on the empty element
  4. Ensure consistent behavior between self-closing and explicit empty tag formats

Tests Added

Added comprehensive tests to verify:

  • Empty elements create proper XmlElement objects instead of null
  • Self-closing and explicit empty elements behave identically
  • Subsequent elements are correctly parsed after empty elements
  • Non-empty elements continue to work as expected

Fixes #80699.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] XML deserialize raw empty-element Fix XmlElement deserialization for empty elements Aug 1, 2025
@Copilot Copilot AI requested a review from StephenMolloy August 1, 2025 08:05
Copilot finished work on behalf of StephenMolloy August 1, 2025 08:05
@jkotas jkotas deleted the copilot/fix-80699 branch August 31, 2025 22:52
@StephenMolloy StephenMolloy restored the copilot/fix-80699 branch September 4, 2025 22:20
@StephenMolloy StephenMolloy reopened this Sep 4, 2025
@StephenMolloy StephenMolloy added this to the 10.0.0 milestone Sep 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

XML deserialize raw empty-element
2 participants