diff --git a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs index fba464fcbf582c..7f58c7a398e273 100644 --- a/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs +++ b/src/libraries/System.Private.Xml/src/System/Xml/Serialization/ReflectionXmlSerializationWriter.cs @@ -152,6 +152,22 @@ private void WriteArray(object o, object? choiceSource, ElementAccessor[] elemen private void WriteArrayItems(ElementAccessor[] elements, TextAccessor? text, ChoiceIdentifierAccessor? choice, object o, object? choiceSources) { + Array? choiceArray = choiceSources as Array; + + if (o is Array array) + { + IEnumerator e = array.GetEnumerator(); + int c = 0; + while (e.MoveNext()) + { + object ai = e.Current; + object? choiceSource = choiceArray?.GetValue(c++); + WriteElements(ai, choiceSource, elements, text, choice, true, true); + } + + return; + } + var arr = o as IList; if (arr != null) @@ -159,7 +175,7 @@ private void WriteArrayItems(ElementAccessor[] elements, TextAccessor? text, Cho for (int i = 0; i < arr.Count; i++) { object? ai = arr[i]; - var choiceSource = ((Array?)choiceSources)?.GetValue(i); + object? choiceSource = choiceArray?.GetValue(i); WriteElements(ai, choiceSource, elements, text, choice, true, true); } } @@ -175,7 +191,7 @@ private void WriteArrayItems(ElementAccessor[] elements, TextAccessor? text, Cho while (e.MoveNext()) { object ai = e.Current; - var choiceSource = ((Array?)choiceSources)?.GetValue(c++); + object? choiceSource = choiceArray?.GetValue(c++); WriteElements(ai, choiceSource, elements, text, choice, true, true); } }