Skip to content

XML deserialize raw empty-element #80699

@KN4CK3R

Description

@KN4CK3R

Description

I need to process some XML input which has HTML code in some tags. For these tags I want the raw content to process it later. I followed this answer on Stackoverflow and used XmlElement as member which works fine in most cases. The only problem I'm facing are self closing tags.

Reproduction Steps

using System.IO;
using System.Xml;
using System.Xml.Serialization;
					
public class Program
{
	public static void Main()
	{
		var serializer = new XmlSerializer(typeof(Root));

		var obj1 = serializer.Deserialize(new StringReader(@"<Root><Description><p>test</p></Description><Name>Test</Name></Root>"));
		// Description: "Element, Name=\"p\""
		// Name: "Test"

		var obj2 = serializer.Deserialize(new StringReader(@"<Root><Description></Description><Name>Test</Name></Root>"));
		// Description: null
		// Name: "Test"

		var obj3 = serializer.Deserialize(new StringReader(@"<Root><Description/><Name>Test</Name></Root>"));
		// Description: "Element, Name=\"Name\""
		// Name: null
	}

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

Expected behavior

obj2 and obj3 should be equal.

Actual behavior

obj1 and obj2 are ok (obj2.Description == "" would be better) but in obj3 the Description member is greedy and contains the Name part.

Regression?

No response

Known Workarounds

No response

Configuration

I have tested it on .NET 5 and .NET 7 without differences.

Other information

No response

Metadata

Metadata

Labels

area-Serializationin-prThere is an active PR which will close this issue when it is merged

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions