public void AddElement (XmlSchemaSequence sequence, MemberInfo member, Type type)
{
//
// Only read/write properties are supported.
//
if (member is PropertyInfo) {
PropertyInfo p = (PropertyInfo) member;
if ((p.CanRead && p.CanWrite) == false)
return;
}
//
// readonly fields are not supported.
//
if (member is FieldInfo) {
FieldInfo f = (FieldInfo) member;
if (f.IsInitOnly || f.IsLiteral)
return;
}
//
// delegates are not supported.
//
if (!type.IsAbstract && typeof (System.Delegate).IsAssignableFrom (type))
return;
//
// If it's an array, write a SchemaType for the type of array
//
if (type.IsArray) {
XmlSchemaType arrayType = WriteArrayType (type, member);
if (arrayType != null)
schema.Items.Add (arrayType);
}
XmlSchemaElement element = new XmlSchemaElement ();
element.Name = member.Name;
XmlQualifiedName schema_type_name = GetQualifiedName (type);
if (type.IsEnum) {
element.SchemaTypeName = new XmlQualifiedName (type.Name);
} else if (schema_type_name.Name == "xml") {
element.SchemaType = WriteComplexSchemaType ();
element.SchemaTypeName = XmlQualifiedName.Empty; // 'xml' is just a temporary name
} else if (schema_type_name == null) {
throw new ArgumentException (String.Format ("The type '{0}' cannot be represented in XML Schema.", type.FullName));
} else // this is the normal case
element.SchemaTypeName = schema_type_name;
object [] attrs = member.GetCustomAttributes (false);
if (attrs.Length > 0) {
foreach (object o in attrs) {
if (o is XmlElementAttribute) {
XmlElementAttribute attr = (XmlElementAttribute) o;
if (attr.DataType != null && attr.DataType.Length != 0)
element.SchemaTypeName = new XmlQualifiedName (attr.DataType, xs);
if (attr.ElementName != null && attr.ElementName.Length != 0)
element.Name = attr.ElementName;
continue;
}
if (o is XmlArrayAttribute) {
if (type.IsArray == false)
throw new ArgumentException (
String.Format ("XmlArrayAttribute is not applicable to {0}, because it is not an array.",
member.Name));
XmlArrayAttribute attr = (XmlArrayAttribute) o;
if (attr.ElementName.Length != 0)
element.Name = attr.ElementName;
continue;
}
//
// isText signals that the mixed="true" in the schema type.
//
if (o is XmlTextAttribute) {
isText = true;
return;
}
if (o is XmlAnyElementAttribute) {
XmlSchemaAny any = new XmlSchemaAny ();
any.MinOccurs = 0;
any.MaxOccursString = "unbounded";
sequence.Items.Add (any);
return;
}
}
}
if (type.IsClass)
element.MinOccurs = 0;
else if (type.IsValueType)
element.MinOccurs = 1;
element.MaxOccurs = 1;
sequence.Items.Add (element);
}