Skip to content

XML serialization of xs:list elements doesn't respect white-space separation #115837

Open
@meixger

Description

@meixger

Description

A [XmlTextAttribute] string[] { get; set; } property with value ["a", "b", "c", "d"] should be serialized as a space-separated list in the xml text.

List datatypes on https://www.w3.org/TR/xmlschema-2/#dt-list

Reproduction Steps

https://dotnetfiddle.net/dPcLua

using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Program
{
	public static void Main()
	{
		// Arrange
		// XML taken from https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.Xml/tests/XmlSchema/TestFiles/StandardTests/xsd10/simpleType/bug102159_1.xml
                var bug102159_1 = """
                                  <?xml version="1.0"?>
                                  <root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="bug102159_1.xsd">
                                    <nms>a b c d</nms>
                                    <mylist>a b c d</mylist>
                                  </root>
                                  """;
		var serializer = new XmlSerializer(typeof(Root));

		// Act
		var instance = (Root)serializer.Deserialize(new StringReader(bug102159_1))!;
			
		// Assert
		if (instance.Nms.Items.Length != 4) throw new InvalidOperationException($"Property Nms contains {instance.Nms.Items.Length} elements.");
		if (instance.MyList.Items.Length != 4) throw new InvalidOperationException($"Property MyList contains {instance.MyList.Items.Length} elements.");
	}
}

[XmlRootAttribute("root")]
public class Root
{
    [XmlElement("nms")]
    public Nms Nms { get; set; }
    
    [XmlElement("mylist")]
    public MyList MyList { get; set; }
}

public class Nms
{
    [XmlText]
    public string[] Items { get; set; }
}

public class MyList
{
    [XmlText]
    public string[] Items { get; set; }
}

Expected behavior

Serialization of string[] should produce a white-space separated simple list.

<root>a b c d</root>

Actual behavior

On serialization the array values are squashed into a string without white-space separators:

<root>abcd</root>

Deserialization should produce an array with values split on white-space.

Regression?

No response

Known Workarounds

Accessing the value via a shadow property and split or join string manually.

https://dotnetfiddle.net/155cRB

Configuration

.NET 9.0

Other information

"The .NET Framework provides accurate bindings for XML attribute declarations with list types, but not for element declarations." source

Related issue #98447

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions