private ElementAccessor ImportElement(XmlSchemaElement element, string identifier, Type desiredMappingType, Type baseType, string ns, bool topLevelElement)
{
if (!element.RefName.IsEmpty)
{
// we cannot re-use the accessor for the element refs
ElementAccessor topAccessor = ImportElement(element.RefName, desiredMappingType, baseType);
if (element.IsMultipleOccurrence && topAccessor.Mapping is ArrayMapping)
{
ElementAccessor refAccessor = topAccessor.Clone();
refAccessor.IsTopLevelInSchema = false;
refAccessor.Mapping.ReferencedByElement = true;
return refAccessor;
}
return topAccessor;
}
if (element.Name.Length == 0)
{
XmlQualifiedName parentType = XmlSchemas.GetParentName(element);
throw new InvalidOperationException(SR.Format(SR.XmlElementHasNoName, parentType.Name, parentType.Namespace));
}
string unescapedName = Accessor.UnescapeName(element.Name);
if (identifier.Length == 0)
identifier = CodeIdentifier.MakeValid(unescapedName);
else
identifier += CodeIdentifier.MakePascal(unescapedName);
TypeMapping mapping = ImportElementType(element, identifier, desiredMappingType, baseType, ns);
ElementAccessor accessor = new ElementAccessor();
accessor.IsTopLevelInSchema = element.Parent is XmlSchema;
accessor.Name = element.Name;
accessor.Namespace = ns;
accessor.Mapping = mapping;
accessor.IsOptional = element.MinOccurs == 0m;
if (element.DefaultValue != null)
{
accessor.Default = element.DefaultValue;
}
else if (element.FixedValue != null)
{
accessor.Default = element.FixedValue;
accessor.IsFixed = true;
}
if (mapping is SpecialMapping && ((SpecialMapping)mapping).NamedAny)
accessor.Any = true;
accessor.IsNullable = element.IsNillable;
if (topLevelElement)
{
accessor.Form = XmlSchemaForm.Qualified;
}
else
{
accessor.Form = ElementForm(ns, element);
}
return accessor;
}