private void WriteElements(object o, object enumSource, ElementAccessor[] elements, TextAccessor text, ChoiceIdentifierAccessor choice, string arrayName, bool writeAccessors, bool isNullable, XmlMapping parentMapping = null)
{
if (elements.Length == 0 && text == null) return;
if (elements.Length == 1 && text == null)
{
WriteElement(o, elements[0], arrayName, writeAccessors, parentMapping);
}
else
{
if (isNullable && choice == null && o == null)
{
return;
}
int anyCount = 0;
var namedAnys = new List<ElementAccessor>();
ElementAccessor unnamedAny = null; // can only have one
string enumTypeName = choice == null ? null : choice.Mapping.TypeDesc.FullName;
for (int i = 0; i < elements.Length; i++)
{
ElementAccessor element = elements[i];
if (element.Any)
{
anyCount++;
if (element.Name != null && element.Name.Length > 0)
namedAnys.Add(element);
else if (unnamedAny == null)
unnamedAny = element;
}
else if (choice != null)
{
if (o != null && o.GetType() == element.Mapping.TypeDesc.Type)
{
WriteElement(o, element, arrayName, writeAccessors);
return;
}
}
else
{
TypeDesc td = element.IsUnbounded ? element.Mapping.TypeDesc.CreateArrayTypeDesc() : element.Mapping.TypeDesc;
if (o.GetType() == td.Type)
{
WriteElement(o, element, arrayName, writeAccessors);
return;
}
}
}
if (anyCount > 0)
{
var elem = o as XmlElement;
if (elem != null)
{
foreach (ElementAccessor element in namedAnys)
{
if (element.Name == elem.Name && element.Namespace == elem.NamespaceURI)
{
WriteElement(elem, element, arrayName, writeAccessors);
return;
}
}
if (choice != null)
{
throw CreateChoiceIdentifierValueException(choice.Mapping.TypeDesc.FullName, choice.MemberName, elem.Name, elem.NamespaceURI);
}
if (unnamedAny != null)
{
WriteElement(elem, unnamedAny, arrayName, writeAccessors);
return;
}
throw CreateUnknownAnyElementException(elem.Name, elem.NamespaceURI);
}
}
if (text != null)
{
bool useReflection = text.Mapping.TypeDesc.UseReflection;
string fullTypeName = text.Mapping.TypeDesc.CSharpName;
WriteText(o, text);
return;
}
if (elements.Length > 0 && o != null)
{
throw CreateUnknownTypeException(o);
}
}
}