Kaynağa Gözat

* XmlSchemaImporter.cs: Implemented ImportTypeMapping and all needed parsing
stuff.
* SoapReflectionImporter.cs: Set the type namespace parameter when creating a map.
* TypeData.cs: Added property that returns a TypeData that represents an array of
the given TypeData.
* TypeTranslator.cs: Added GetArrayName() method.
Added TypeDatas for missing primitive types.
* XmlCodeExporter.cs: Implemented ExportMembersMapping. Fixed generation of
XmlRootAttribute. Added the namespace to all attributes being generated.
Other fixes in the generation of code.
* XmlReflectionImporter: store the namespace of the type in the maps.
* XmlSchemaExporter.cs: Several fixes. Only set the "mixed" attribute if the
class can generate text. Do not export inherited attributes of a class.
Use the new root namespace stored in the map when generating the root element.
* XmlSerializationWriter: Always write a prefix when writing a qname, even if the
namespace is the default namespace.
* XmlSerializationWriterInterpreter.cs: fixed missing "else".
* XmlTypeMapElementInfo.cs: In DataTypeNamespace property, return the type
namespace instead of the map namespace (which can be different if the type
has a XmlRoot element).
* XmlTypeMapMember.cs: Set the default value of the DefaultValue property
to System.DBNull.Value.
* XmlTypeMapMemberElement.cs: ElementInfo property: In the getter Create
the collection if it has not yet been created.
* XmlTypeMapping.cs: Added property XmlTypeNamespace which stores the namespace
of the type. It may be different from the namespace of the map if the type
has a XmlRoot element. Also added IsSimpleType property.
In ClassMap, added AllMembers property.

svn path=/trunk/mcs/; revision=17075

Lluis Sanchez 22 yıl önce
ebeveyn
işleme
4de0bcd2ae

+ 31 - 0
mcs/class/System.XML/System.Xml.Serialization/ChangeLog

@@ -1,3 +1,34 @@
+2003-08-05  Lluis Sanchez Gual <[email protected]>
+
+	* XmlSchemaImporter.cs: Implemented ImportTypeMapping and all needed parsing
+	  stuff.
+	* SoapReflectionImporter.cs: Set the type namespace parameter when creating a map.
+	* TypeData.cs: Added property that returns a TypeData that represents an array of
+	  the given TypeData.
+	* TypeTranslator.cs: Added GetArrayName() method.
+	  Added TypeDatas for missing primitive types.
+	* XmlCodeExporter.cs: Implemented ExportMembersMapping. Fixed generation of
+	  XmlRootAttribute. Added the namespace to all attributes being generated.
+	  Other fixes in the generation of code.
+	* XmlReflectionImporter: store the namespace of the type in the maps.
+	* XmlSchemaExporter.cs: Several fixes. Only set the "mixed" attribute if the
+	  class can generate text. Do not export inherited attributes of a class.
+	  Use the new root namespace stored in the map when generating the root element.
+	* XmlSerializationWriter: Always write a prefix when writing a qname, even if the
+	  namespace is the default namespace.
+	* XmlSerializationWriterInterpreter.cs: fixed missing "else".
+	* XmlTypeMapElementInfo.cs: In DataTypeNamespace property, return the type
+	  namespace instead of the map namespace (which can be different if the type
+	  has a XmlRoot element).
+	* XmlTypeMapMember.cs: Set the default value of the DefaultValue property
+	  to System.DBNull.Value.
+	* XmlTypeMapMemberElement.cs: ElementInfo property: In the getter Create
+	  the collection if it has not yet been created.
+	* XmlTypeMapping.cs: Added property XmlTypeNamespace which stores the namespace
+	  of the type. It may be different from the namespace of the map if the type
+	  has a XmlRoot element. Also added IsSimpleType property.
+	  In ClassMap, added AllMembers property.
+	
 2003-07-30  Lluis Sanchez Gual <[email protected]>
 
 	* TypeData.cs: Added new constructor and variables to allow the creation of

+ 2 - 4
mcs/class/System.XML/System.Xml.Serialization/SoapReflectionImporter.cs

@@ -108,7 +108,7 @@ namespace System.Xml.Serialization {
 		XmlTypeMapping CreateTypeMapping (TypeData typeData, string defaultXmlType, string defaultNamespace)
 		{
 			string membersNamespace = defaultNamespace;
-			string elementName;
+
 			SoapAttributes atts = null;
 			if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
 
@@ -133,10 +133,8 @@ namespace System.Xml.Serialization {
 					defaultXmlType = atts.SoapType.TypeName;
 			}
 
-			elementName = defaultXmlType;
-
 			if (membersNamespace == null) membersNamespace = "";
-			XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
+			XmlTypeMapping map = new XmlTypeMapping (defaultXmlType, membersNamespace, typeData, defaultXmlType, membersNamespace);
 
 			relatedMaps.Add (map);
 			return map;

+ 18 - 1
mcs/class/System.XML/System.Xml.Serialization/TypeData.cs

@@ -23,6 +23,7 @@ namespace System.Xml.Serialization
 		string typeName;
 		string fullTypeName;
 		TypeData listItemTypeData;
+		TypeData listTypeData;
 
 		public TypeData (Type type, string elementName, bool isPrimitive)
 		{
@@ -48,7 +49,7 @@ namespace System.Xml.Serialization
 			}
 		}
 
-		public TypeData (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
+		internal TypeData (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
 		{
 			this.elementName = xmlType;
 			this.typeName = typeName;
@@ -145,6 +146,22 @@ namespace System.Xml.Serialization
 			}
 		}
 
+		public TypeData ListTypeData
+		{
+			get
+			{
+				if (listTypeData != null) return listTypeData;
+				
+				listTypeData = new TypeData (TypeName + "[]",
+					FullTypeName + "[]",
+					TypeTranslator.GetArrayName(XmlType),
+					SchemaTypes.Array, this);
+
+				return listTypeData;
+			}
+		}
+
+
 		public static PropertyInfo GetIndexerProperty (Type collectionType)
 		{
 			PropertyInfo[] props = collectionType.GetProperties (BindingFlags.Instance | BindingFlags.Public);

+ 14 - 2
mcs/class/System.XML/System.Xml.Serialization/TypeTranslator.cs

@@ -4,7 +4,7 @@
 // Authors:
 //	Gonzalo Paniagua Javier ([email protected])
 //	Erik LeBel ([email protected])
-//  Lluis Sanchez Gual ([email protected])
+//  Lluis Sanchez Gual ([email protected])
 //
 // (C) 2002 Ximian, Inc (http://www.ximian.com)
 // (C) 2003 Erik Lebel
@@ -54,6 +54,7 @@ namespace System.Xml.Serialization
 			primitiveTypes.Add ("time", new TypeData (typeof (DateTime), "time", true));
 			primitiveTypes.Add ("NMTOKEN", new TypeData (typeof (string), "NMTOKEN", true));
 			primitiveTypes.Add ("NCName", new TypeData (typeof (string), "NCName", true));
+			primitiveTypes.Add ("language", new TypeData (typeof (string), "language", true));
 		}
 
 		public static TypeData GetTypeData (Type type)
@@ -71,7 +72,7 @@ namespace System.Xml.Serialization
 			string name;
 			if (type.IsArray) {
 				string sufix = GetTypeData (type.GetElementType ()).XmlType;
-				name = "ArrayOf" + Char.ToUpper (sufix [0]) + sufix.Substring (1);
+				name = GetArrayName (sufix);
 			}
 			else 
 				name = type.Name;
@@ -92,6 +93,17 @@ namespace System.Xml.Serialization
 			if (td == null) throw new NotSupportedException ("Data type '" + typeName + "' not supported");
 			return td;
 		}
+
+		public static TypeData CreateCustomType (string typeName, string fullTypeName, string xmlType, SchemaTypes schemaType, TypeData listItemTypeData)
+		{
+			TypeData td = new TypeData (typeName, fullTypeName, xmlType, schemaType, listItemTypeData);
+			return td;
+		}
+
+		public static string GetArrayName (string elemName)
+		{
+			return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
+		}
 	}
 }
 

+ 64 - 37
mcs/class/System.XML/System.Xml.Serialization/XmlCodeExporter.cs

@@ -10,6 +10,7 @@
 
 using System.CodeDom;
 using System.Collections;
+using System.Xml.Schema;
 
 namespace System.Xml.Serialization {
 	public class XmlCodeExporter {
@@ -20,7 +21,7 @@ namespace System.Xml.Serialization {
 		CodeCompileUnit codeCompileUnit;
 		CodeAttributeDeclarationCollection includeMetadata;
 
-		Hashtable exportedMaps;
+		Hashtable exportedMaps = new Hashtable ();
 
 		#endregion
 
@@ -69,15 +70,14 @@ namespace System.Xml.Serialization {
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
 		{
-			throw new NotImplementedException ();
+			CodeTypeDeclaration dummyClass = new CodeTypeDeclaration ();
+			ExportMembersMapCode (dummyClass, (ClassMap)xmlMembersMapping.ObjectMap, xmlMembersMapping.Namespace, null);
 		}
 
 		public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
 		{
-			exportedMaps = new Hashtable ();
 			ExportMapCode (xmlTypeMapping, true);
 		}
 
@@ -113,22 +113,31 @@ namespace System.Xml.Serialization {
 			CodeTypeDeclaration codeClass = new CodeTypeDeclaration (map.TypeData.TypeName);
 			codeClass.Attributes = MemberAttributes.Public;
 
-			CodeAttributeDeclaration att = new CodeAttributeDeclaration (isRoot ? "XmlRoot" : "XmlTypeAttribute");
-			if (map.ElementName != map.TypeData.TypeName) att.Arguments.Add (GetArg ("Name", map.ElementName));
-			if (map.Namespace != "") att.Arguments.Add (GetArg ("Namespace", map.Namespace));
+			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlType");
+			if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
+			if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
 			AddCustomAttribute (codeClass, att, false);
 
+			if (isRoot) {
+				CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRoot");
+				if (map.ElementName != map.TypeData.TypeName) ratt.Arguments.Add (GetArg (map.ElementName));
+				if (map.Namespace != "") ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
+				AddCustomAttribute (codeClass, ratt, false);
+			}
+			
 			if (map.BaseMap != null)
 			{
 				CodeTypeReference ctr = new CodeTypeReference (map.BaseMap.TypeData.FullTypeName);
 				codeClass.BaseTypes.Add (ctr);
+				ExportMapCode (map.BaseMap, false);
 			}
 
 			foreach (XmlTypeMapping tm in map.DerivedTypes)
 			{
-				CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("XmlInclude");
+				CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlInclude");
 				iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(tm.TypeData.FullTypeName)));
 				AddCustomAttribute (codeClass, iatt, true);
+				ExportMapCode (tm, false);
 			}
 
 			AddCodeType (codeClass);
@@ -174,16 +183,19 @@ namespace System.Xml.Serialization {
 			ICollection attributes = map.AttributeMembers;
 			if (attributes != null)
 			{
-				foreach (XmlTypeMapMemberAttribute attr in attributes)
+				foreach (XmlTypeMapMemberAttribute attr in attributes) {
+					if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
 					AddAttributeFieldMember (codeClass, attr, defaultNamespace);
+				}
 			}
 
 			XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
 			if (anyAttrMember != null)
 			{
 				CodeMemberField codeField = new CodeMemberField (anyAttrMember.TypeData.FullTypeName, anyAttrMember.Name);
+				codeField.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
 				codeField.Attributes = MemberAttributes.Public;
-				AddCustomAttribute (codeField, "XmlAnyAttribute");
+				AddCustomAttribute (codeField, "System.Xml.Serialization.XmlAnyAttribute");
 				codeClass.Members.Add (codeField);
 			}
 		}
@@ -192,6 +204,7 @@ namespace System.Xml.Serialization {
 		{
 			CodeMemberField codeField = new CodeMemberField (type, name);
 			codeField.Attributes = MemberAttributes.Public;
+			codeField.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
 
 			if (defaultValue != System.DBNull.Value)
 			{
@@ -206,9 +219,10 @@ namespace System.Xml.Serialization {
 			CodeMemberField codeField = CreateFieldMember (attinfo.TypeData.FullTypeName, attinfo.Name, attinfo.DefaultValue);
 			codeClass.Members.Add (codeField);
 
-			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlAttribute");
-			if (attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg ("Name", attinfo.AttributeName));
+			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAttribute");
+			if (attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
 			if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
+			if (attinfo.Form != XmlSchemaForm.None) att.Arguments.Add (GetArg ("Form",attinfo.Form));
 			AddCustomAttribute (codeField, att, true);
 
 			if (attinfo.MappedType != null)
@@ -217,6 +231,7 @@ namespace System.Xml.Serialization {
 
 		void AddElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
 		{
+			if (member.TypeData == null) Console.WriteLine ("NN:" + member.Name);
 			CodeMemberField codeField = CreateFieldMember (member.TypeData.FullTypeName, member.Name, member.DefaultValue);
 			codeClass.Members.Add (codeField);
 			TypeData defaultType = member.TypeData;
@@ -230,22 +245,11 @@ namespace System.Xml.Serialization {
 
 			foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
 			{
-				if (einfo.IsTextElement) {
-					CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("XmlText");
-					if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) uatt.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
-					AddCustomAttribute (codeField, uatt, true);
-					continue;
-				}
-
-				if (einfo.IsUnnamedAnyElement) {
-					CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("XmlAnyElement");
-					if (einfo.Namespace != defaultNamespace) uatt.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
-					AddCustomAttribute (codeField, uatt, true);
+				if (ExportExtraElementAttributes (codeField, einfo, defaultNamespace, defaultType))
 					continue;
-				}
 
-				CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlElement");
-				if (einfo.ElementName != member.Name) att.Arguments.Add (GetArg ("ElementName", einfo.ElementName));
+				CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlElement");
+				if (einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
 				if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) att.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
 				if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
 				if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
@@ -253,6 +257,9 @@ namespace System.Xml.Serialization {
 
 				if (einfo.MappedType != null) ExportMapCode (einfo.MappedType, false);
 			}
+
+			if (member.ChoiceMember != null)
+				AddCustomAttribute (codeField, "System.Xml.Serialization.XmlChoiceIdentifier", GetArg(member.ChoiceMember));
 		}
 
 		void AddAnyElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberElement member, string defaultNamespace)
@@ -262,14 +269,11 @@ namespace System.Xml.Serialization {
 
 			foreach (XmlTypeMapElementInfo einfo in member.ElementInfo)
 			{
-				CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlAnyElement");
-				if (!einfo.IsUnnamedAnyElement) att.Arguments.Add (GetArg ("Name", einfo.ElementName));
-				if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
-				AddCustomAttribute (codeField, att, true);
+				ExportExtraElementAttributes (codeField, einfo, defaultNamespace, einfo.TypeData);
 			}
 		}
 
-		bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMemberElement member)
+		bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
 		{
 			if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
 				return true;
@@ -282,11 +286,12 @@ namespace System.Xml.Serialization {
 		void AddArrayElementFieldMember (CodeTypeDeclaration codeClass, XmlTypeMapMemberList member, string defaultNamespace)
 		{
 			CodeMemberField codeField = new CodeMemberField (member.TypeData.FullTypeName, member.Name);
+			codeField.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
 			codeField.Attributes = MemberAttributes.Public;
 			codeClass.Members.Add (codeField);
 			XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
 
-			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlArray");
+			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArray");
 			if (einfo.ElementName != member.Name) att.Arguments.Add (GetArg ("ElementName", einfo.ElementName));
 			if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
 			if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
@@ -308,9 +313,9 @@ namespace System.Xml.Serialization {
 				bool needsType = (listMap.ItemInfo.Count > 1) ||
 								 (ainfo.TypeData.FullTypeName != type.FullTypeName && !listMap.IsMultiArray);
 
-				CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlArrayItem");
+				CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArrayItem");
 				if (ainfo.ElementName != defaultName) att.Arguments.Add (GetArg ("ElementName", ainfo.ElementName));
-				if (ainfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", ainfo.Namespace));
+				if (ainfo.Namespace != defaultNamespace && ainfo.Namespace != XmlSchema.Namespace) att.Arguments.Add (GetArg ("Namespace", ainfo.Namespace));
 				if (needsType) att.Arguments.Add (GetTypeArg ("Type", ainfo.TypeData.FullTypeName));
 				if (ainfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
 				if (att.Arguments.Count > 0 && nestingLevel > 0) att.Arguments.Add (GetArg ("NestingLevel", nestingLevel));
@@ -334,7 +339,25 @@ namespace System.Xml.Serialization {
 				if (ainfo.MappedType != null) ExportMapCode (ainfo.MappedType, false);
 			}
 		}
-		
+
+		bool ExportExtraElementAttributes (CodeMemberField codeField, XmlTypeMapElementInfo einfo, string defaultNamespace, TypeData defaultType)
+		{
+			if (einfo.IsTextElement) {
+				CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlText");
+				if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) uatt.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
+				AddCustomAttribute (codeField, uatt, true);
+				return true;
+			}
+			else if (einfo.IsUnnamedAnyElement) {
+				CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
+				if (!einfo.IsUnnamedAnyElement) uatt.Arguments.Add (GetArg ("Name", einfo.ElementName));
+				if (einfo.Namespace != defaultNamespace) uatt.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
+				AddCustomAttribute (codeField, uatt, true);
+				return true;
+			}
+			return false;
+		}
+
 		void ExportEnumCode (XmlTypeMapping map)
 		{
 			if (IsMapExported (map)) return;
@@ -345,7 +368,7 @@ namespace System.Xml.Serialization {
 			codeEnum.IsEnum = true;
 			AddCodeType (codeEnum);
 
-			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("XmlTypeAttribute");
+			CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTypeAttribute");
 			if (map.ElementName != map.TypeData.TypeName) att.Arguments.Add (GetArg ("Name", map.ElementName));
 			if (map.Namespace != "") att.Arguments.Add (GetArg ("Namespace", map.Namespace));
 			AddCustomAttribute (codeEnum, att, false);
@@ -355,10 +378,13 @@ namespace System.Xml.Serialization {
 			foreach (EnumMap.EnumMapMember emem in emap.Members)
 			{
 				CodeMemberField codeField = new CodeMemberField ("", emem.EnumName);
+				codeField.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
+
 				if (emem.EnumName != emem.XmlName)
 				{
-					CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("XmlEnum");
+					CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlEnum");
 					xatt.Arguments.Add (GetArg ("Name", emem.XmlName));
+
 					AddCustomAttribute (codeField, xatt, true);
 				}
 				codeEnum.Members.Add (codeField);
@@ -408,6 +434,7 @@ namespace System.Xml.Serialization {
 
 		void AddCodeType (CodeTypeDeclaration type)
 		{
+			type.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
 			codeNamespace.Types.Add (type);
 		}
 

+ 6 - 10
mcs/class/System.XML/System.Xml.Serialization/XmlReflectionImporter.cs

@@ -124,7 +124,7 @@ namespace System.Xml.Serialization {
 
 		XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
 		{
-			string membersNamespace = defaultNamespace;
+			string membersNamespace;
 			string elementName;
 			XmlAttributes atts = null;
 			if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
@@ -147,12 +147,13 @@ namespace System.Xml.Serialization {
 			if (atts.XmlType != null)
 			{
 				if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
-					membersNamespace = atts.XmlType.Namespace;
+					defaultNamespace = atts.XmlType.Namespace;
 
 				if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
 					defaultXmlType = atts.XmlType.TypeName;
 			}
 
+			membersNamespace = defaultNamespace;
 			elementName = defaultXmlType;
 
 			if (root != null)
@@ -164,7 +165,7 @@ namespace System.Xml.Serialization {
 			}
 
 			if (membersNamespace == null) membersNamespace = "";
-			XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
+			XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType, defaultNamespace);
 			return map;
 		}
 
@@ -322,8 +323,8 @@ namespace System.Xml.Serialization {
 			else
 			{
 				XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
-				if (elem.MappedType != null) baseName = GetArrayName (elem.MappedType.ElementName);
-				else baseName = GetArrayName (elem.ElementName);
+				if (elem.MappedType != null) baseName = TypeTranslator.GetArrayName (elem.MappedType.ElementName);
+				else baseName = TypeTranslator.GetArrayName (elem.ElementName);
 			}
 
 			// Avoid name colisions
@@ -350,11 +351,6 @@ namespace System.Xml.Serialization {
 			return map;
 		}
 
-		string GetArrayName (string elemName)
-		{
-			return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
-		}
-
 		XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
 		{
 			XmlTypeMapping map = helper.GetRegisteredClrType (type, GetTypeNamespace (TypeTranslator.GetTypeData (type), root, defaultNamespace));

+ 19 - 20
mcs/class/System.XML/System.Xml.Serialization/XmlSchemaExporter.cs

@@ -3,6 +3,7 @@
 //
 // Author:
 //   Tim Coleman ([email protected])
+//   Lluis Sanchez Gual ([email protected])
 //
 // Copyright (C) Tim Coleman, 2002
 //
@@ -17,7 +18,7 @@ namespace System.Xml.Serialization {
 		#region Fields
 
 		XmlSchemas schemas;
-		Hashtable exportedMaps;
+		Hashtable exportedMaps = new Hashtable();
 
 		#endregion
 
@@ -40,7 +41,6 @@ namespace System.Xml.Serialization {
 
 		public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
 		{
-			exportedMaps = new Hashtable ();
 			XmlSchema schema = GetSchema (xmlMembersMapping.Namespace);
 
 			XmlSchemaElement selem = new XmlSchemaElement ();
@@ -68,15 +68,14 @@ namespace System.Xml.Serialization {
 
 		public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
 		{
-			exportedMaps = new Hashtable ();
 			XmlSchema schema = GetSchema (null);
 			XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (null, xmlTypeMapping.TypeData);
-			einfo.Namespace = "";
+			einfo.Namespace = xmlTypeMapping.Namespace;
 			einfo.ElementName = xmlTypeMapping.ElementName;
 			einfo.MappedType = xmlTypeMapping;
 			einfo.IsNullable = false;
 			AddSchemaElement (schema.Items, schema, einfo, false);
-			CompileSchemas ();
+//			CompileSchemas ();
 		}
 
 		void ExportClassSchema (XmlTypeMapping map)
@@ -84,16 +83,15 @@ namespace System.Xml.Serialization {
 			if (IsMapExported (map)) return;
 			SetMapExported (map);
 
-			XmlSchema schema = GetSchema (map.Namespace);
+			XmlSchema schema = GetSchema (map.XmlTypeNamespace);
 			XmlSchemaComplexType stype = new XmlSchemaComplexType ();
-			stype.Name = map.ElementName;
+			stype.Name = map.XmlType;
 			schema.Items.Add (stype);
 			if (map.BaseMap != null)
 			{
 				XmlSchemaComplexContent cstype = new XmlSchemaComplexContent ();
-				cstype.IsMixed = true;
 				XmlSchemaComplexContentExtension ext = new XmlSchemaComplexContentExtension ();
-				ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.Namespace);
+				ext.BaseTypeName = new XmlQualifiedName (map.BaseMap.XmlType, map.BaseMap.XmlTypeNamespace);
 				cstype.Content = ext;
 				stype.ContentModel = cstype;
 
@@ -113,7 +111,7 @@ namespace System.Xml.Serialization {
 				ExportMembersMapSchema (schema, (ClassMap)map.ObjectMap, map.BaseMap, stype.Attributes, out particle, out anyAttribute);
 				stype.Particle = particle;
 				stype.AnyAttribute = anyAttribute;
-				stype.IsMixed = true;
+				stype.IsMixed = ((ClassMap)map.ObjectMap).XmlTextCollector != null;
 			}
 		}
 
@@ -163,8 +161,10 @@ namespace System.Xml.Serialization {
 			ICollection attributes = map.AttributeMembers;
 			if (attributes != null)
 			{
-				foreach (XmlTypeMapMemberAttribute attr in attributes) 
+				foreach (XmlTypeMapMemberAttribute attr in attributes) {
+					if (baseMap != null && DefinedInBaseMap (baseMap, attr)) continue;
 					outAttributes.Add (GetSchemaAttribute (schema, attr));
+				}
 			}
 
 			XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
@@ -249,20 +249,20 @@ namespace System.Xml.Serialization {
 						break;
 
 					case SchemaTypes.Enum:
-						selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.Namespace);
+						selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);
 						ImportNamespace (currentSchema, einfo.MappedType.Namespace);
 						ExportEnumSchema (einfo.MappedType);
 						break;
 
 					case SchemaTypes.Array: 
-						selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.Namespace);;
+						selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);;
 						ImportNamespace (currentSchema, einfo.MappedType.Namespace);
 						ExportArraySchema (einfo.MappedType); 
 						break;
 
 					case SchemaTypes.Class:
 						if (einfo.MappedType.TypeData.Type != typeof(object)) {
-							selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.Namespace);;
+							selem.SchemaTypeName = new XmlQualifiedName (einfo.MappedType.XmlType, einfo.MappedType.XmlTypeNamespace);;
 							ImportNamespace (currentSchema, einfo.MappedType.Namespace);
 							ExportClassSchema (einfo.MappedType);
 						}
@@ -293,7 +293,7 @@ namespace System.Xml.Serialization {
 			schema.Includes.Add (imp);
 		}
 
-		bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMemberElement member)
+		bool DefinedInBaseMap (XmlTypeMapping map, XmlTypeMapMember member)
 		{
 			if (((ClassMap)map.ObjectMap).FindMember (member.Name) != null)
 				return true;
@@ -344,8 +344,8 @@ namespace System.Xml.Serialization {
 			if (numInfos == 1)
 			{
 				XmlSchemaParticle selem = AddSchemaElement (destcol, currentSchema, (XmlTypeMapElementInfo) infos[infos.Count-1], true);
-				if (selem.MinOccursString == string.Empty) selem.MinOccursString = "0";
-				if (selem.MaxOccursString == string.Empty) selem.MaxOccursString = "unbounded";
+				selem.MinOccursString = "0";
+				selem.MaxOccursString = "unbounded";
 				return selem;
 			}
 			else
@@ -393,7 +393,6 @@ namespace System.Xml.Serialization {
 
 			XmlSchema schema = GetSchema (map.Namespace);
 			XmlSchemaComplexType stype = new XmlSchemaComplexType ();
-			stype.IsMixed = true;
 			stype.Name = map.ElementName;
 			schema.Items.Add (stype);
 
@@ -420,8 +419,8 @@ namespace System.Xml.Serialization {
 
 		void CompileSchemas ()
 		{
-			foreach (XmlSchema sc in schemas)
-				sc.Compile (null);
+//			foreach (XmlSchema sc in schemas)
+//				sc.Compile (null);
 		}
 
 		XmlSchema GetSchema (string ns)

+ 934 - 3
mcs/class/System.XML/System.Xml.Serialization/XmlSchemaImporter.cs

@@ -1,5 +1,5 @@
 // 
-// System.Xml.Serialization.XmlSchemaImporter 
+// System.Xml.Serialization.XmlSchemaImporter
 //
 // Author:
 //   Tim Coleman ([email protected])
@@ -8,6 +8,8 @@
 //
 
 using System.Xml;
+using System.Xml.Schema;
+using System.Collections;
 
 namespace System.Xml.Serialization {
 	public class XmlSchemaImporter {
@@ -16,6 +18,20 @@ namespace System.Xml.Serialization {
 
 		XmlSchemas schemas;
 		CodeIdentifiers typeIdentifiers;
+		CodeIdentifiers elemIdentifiers = new CodeIdentifiers ();
+		Hashtable mappedTypes = new Hashtable ();
+		Hashtable dataMappedTypes = new Hashtable ();
+		Queue pendingMaps = new Queue ();
+
+		static readonly XmlQualifiedName anyType = new XmlQualifiedName ("anyType",XmlSchema.Namespace);
+		XmlSchemaElement anyElement = null;
+
+		class MapFixup
+		{
+			public XmlTypeMapping Map;
+			public XmlSchemaComplexType SchemaType;
+			public XmlQualifiedName TypeName;
+		}
 
 		#endregion
 
@@ -24,6 +40,7 @@ namespace System.Xml.Serialization {
 		public XmlSchemaImporter (XmlSchemas schemas)
 		{
 			this.schemas = schemas;
+			typeIdentifiers = new CodeIdentifiers ();
 		}
 
 		public XmlSchemaImporter (XmlSchemas schemas, CodeIdentifiers typeIdentifiers)
@@ -80,10 +97,924 @@ namespace System.Xml.Serialization {
 			throw new NotImplementedException ();
 		}
 
-		[MonoTODO]
 		public XmlTypeMapping ImportTypeMapping (XmlQualifiedName name)
 		{
-			throw new NotImplementedException ();
+			XmlSchemaElement elem = (XmlSchemaElement) schemas.Find (name, typeof (XmlSchemaElement));
+			if (elem == null) return null;
+
+			// The root element must be an element with complex type
+
+			XmlQualifiedName qname;
+			XmlSchemaType stype;
+			if (elem.SchemaType != null)
+			{
+				stype = elem.SchemaType;
+				qname = name;
+			}
+			else
+			{
+				if (elem.SchemaTypeName.IsEmpty) return null;
+				if (elem.SchemaTypeName.Namespace == XmlSchema.Namespace) return null;
+				object type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaComplexType));
+				if (type == null) type = schemas.Find (elem.SchemaTypeName, typeof (XmlSchemaSimpleType));
+				if (type == null) throw new InvalidOperationException ("Schema type '" + elem.SchemaTypeName + "' not found");
+				stype = (XmlSchemaType) type;
+				qname = stype.QualifiedName;
+			}
+
+			if (stype is XmlSchemaSimpleType) return null;
+
+			XmlTypeMapping map = ImportType (qname, (XmlSchemaComplexType)stype, name);
+			BuildPendingMaps ();
+			return map;
+		}
+
+		public XmlTypeMapping ImportType (XmlQualifiedName name, XmlQualifiedName root)
+		{
+			XmlTypeMapping map = GetRegisteredTypeMapping (name);
+			if (map != null) return map;
+
+			XmlSchemaType type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaComplexType));
+			if (type == null) type = (XmlSchemaType) schemas.Find (name, typeof (XmlSchemaSimpleType));
+
+			return ImportType (name, type, root);
+		}
+
+		XmlTypeMapping ImportType (XmlQualifiedName name, XmlSchemaType stype, XmlQualifiedName root)
+		{
+			XmlTypeMapping map = GetRegisteredTypeMapping (name);
+			if (map != null) return map;
+
+			if (stype is XmlSchemaComplexType)
+				return ImportClassComplexType (name, (XmlSchemaComplexType) stype, root);
+			else if (stype is XmlSchemaSimpleType)
+				return ImportClassSimpleType (name, (XmlSchemaSimpleType) stype, root);
+
+			throw new NotSupportedException ("Schema type not supported: " + stype.GetType ());
+		}
+
+		XmlTypeMapping ImportClassComplexType (XmlQualifiedName typeQName, XmlSchemaComplexType stype, XmlQualifiedName root)
+		{
+//			if (root != null) typeQName = root;
+
+			XmlTypeMapping map;
+
+			// The need for fixups: If the complex type is an array, then to get the type of the
+			// array we need first to get the type of the items of the array.
+			// But if one of the item types or its children has a referece to this type array,
+			// then we enter in an infinite loop. This does not happen with class types because
+			// the class map is registered before parsing the children. We can't do the same
+			// with the array type because to register the array map we need the type of the array.
+
+			if (root == null && CanBeArray (typeQName, stype))
+			{
+				TypeData typeData;
+				ListMap listMap = BuildArrayMap (typeQName, stype, out typeData);
+				if (listMap != null)
+				{
+					map = CreateArrayTypeMapping (typeQName, typeData);
+					map.ObjectMap = listMap;
+					return map;
+				}
+
+				// After all, it is not an array. Create a class map then.
+			}
+			else if (CanBeAnyElement (stype))
+			{
+				return GetTypeMapping (TypeTranslator.GetTypeData(typeof(XmlElement)));
+			}
+			else if (CanBeIXmlSerializable (stype))
+			{
+				return GetTypeMapping (TypeTranslator.GetTypeData(typeof(object)));
+			}
+
+			// Register the map right now but do not build it,
+			// This will avoid loops.
+
+			map = CreateTypeMapping (typeQName, SchemaTypes.Class, root);
+			RegisterMapFixup (map, typeQName, stype);
+			return map;
+		}
+
+		void RegisterMapFixup (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
+		{
+			MapFixup fixup = new MapFixup ();
+			fixup.Map = map;
+			fixup.SchemaType = stype;
+			fixup.TypeName = typeQName;
+			pendingMaps.Enqueue (fixup);
+		}
+
+		void BuildPendingMaps ()
+		{
+			while (pendingMaps.Count > 0) {
+				MapFixup fixup  = (MapFixup) pendingMaps.Dequeue ();
+				if (fixup.Map.ObjectMap == null) {
+					BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
+					if (fixup.Map.ObjectMap == null) pendingMaps.Enqueue (fixup);
+				}
+			}
+		}
+
+		void BuildPendingMap (XmlTypeMapping map)
+		{
+			if (map.ObjectMap != null) return;
+
+			foreach (MapFixup fixup in pendingMaps)
+			{
+				if (fixup.Map == map) {
+					BuildClassMap (fixup.Map, fixup.TypeName, fixup.SchemaType);
+					return;
+				}
+			}
+			throw new InvalidOperationException ("Can't complete map of type " + map.XmlType + " : " + map.Namespace);
+		}
+
+		void BuildClassMap (XmlTypeMapping map, XmlQualifiedName typeQName, XmlSchemaComplexType stype)
+		{
+			CodeIdentifiers classIds = new CodeIdentifiers();
+			classIds.AddReserved (map.TypeData.TypeName);
+
+			ClassMap cmap = new ClassMap ();
+			map.ObjectMap = cmap;
+			bool isMixed = stype.IsMixed;
+
+			if (stype.Particle != null)
+			{
+				ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, isMixed);
+			}
+			else
+			{
+				if (stype.ContentModel is XmlSchemaSimpleContent) {
+					ImportSimpleContent (typeQName, map, (XmlSchemaSimpleContent)stype.ContentModel, classIds, isMixed);
+				}
+				else if (stype.ContentModel is XmlSchemaComplexContent) {
+					ImportComplexContent (typeQName, map, (XmlSchemaComplexContent)stype.ContentModel, classIds, isMixed);
+				}
+			}
+
+			ImportAttributes (typeQName, cmap, stype.Attributes, stype.AnyAttribute, classIds);
+		}
+
+		void ImportAttributes (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection atts, XmlSchemaAnyAttribute anyat, CodeIdentifiers classIds)
+		{
+			if (anyat != null)
+			{
+    			XmlTypeMapMemberAnyAttribute member = new XmlTypeMapMemberAnyAttribute ();
+				member.Name = classIds.AddUnique ("AnyAttribute", member);
+				member.TypeData = TypeTranslator.GetTypeData (typeof(XmlAttribute[]));
+				cmap.AddMember (member);
+			}
+			
+			foreach (XmlSchemaObject at in atts)
+			{
+				if (at is XmlSchemaAttribute)
+				{
+					string ns;
+					XmlSchemaAttribute attr = (XmlSchemaAttribute)at;
+					XmlSchemaAttribute refAttr = GetRefAttribute (typeQName, attr, out ns);
+					XmlTypeMapMemberAttribute member = new XmlTypeMapMemberAttribute ();
+					member.Name = classIds.AddUnique (CodeIdentifier.MakeValid (refAttr.Name), member);
+					member.AttributeName = refAttr.Name;
+					member.Namespace = ns;
+					member.Form = refAttr.Form;
+					member.TypeData = GetAttributeTypeData (typeQName, attr);
+					if (refAttr.DefaultValue != null) member.DefaultValue = refAttr.DefaultValue;
+					if (member.TypeData.IsComplexType)
+						member.MappedType = GetTypeMapping (member.TypeData);
+					cmap.AddMember (member);
+				}
+				else if (at is XmlSchemaAttributeGroupRef)
+				{
+					XmlSchemaAttributeGroupRef gref = (XmlSchemaAttributeGroupRef)at;
+					XmlSchemaAttributeGroup grp = (XmlSchemaAttributeGroup) schemas.Find (gref.RefName, typeof(XmlSchemaAttributeGroup));
+					ImportAttributes (typeQName, cmap, grp.Attributes, grp.AnyAttribute, classIds);
+				}
+			}
+		}
+
+		ListMap BuildArrayMap (XmlQualifiedName typeQName, XmlSchemaComplexType stype, out TypeData arrayTypeData)
+		{
+			ClassMap cmap = new ClassMap ();
+			CodeIdentifiers classIds = new CodeIdentifiers();
+			ImportParticleComplexContent (typeQName, cmap, stype.Particle, classIds, false);
+
+			XmlTypeMapMemberFlatList list = (cmap.AllMembers.Count == 1) ? cmap.AllMembers[0] as XmlTypeMapMemberFlatList : null;
+			if (list != null && list.ChoiceMember == null)
+			{
+				arrayTypeData = list.TypeData;
+				return list.ListMap;
+			}
+			else
+			{
+				arrayTypeData = null;
+				return null;
+			}
+		}
+
+		void ImportParticleComplexContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool isMixed)
+		{
+			ImportParticleContent (typeQName, cmap, particle, classIds, false, ref isMixed);
+			if (isMixed)
+			{
+				XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
+				member.Name = classIds.AddUnique ("Text", member);
+				member.TypeData = TypeTranslator.GetTypeData (typeof(string[]));
+				member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData.ListItemTypeData));
+				member.IsXmlTextCollector = true;
+				member.ListMap = new ListMap ();
+				member.ListMap.ItemInfo = member.ElementInfo;
+				cmap.AddMember (member);
+			}
+		}
+		
+		void ImportParticleContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaParticle particle, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
+		{
+			if (particle is XmlSchemaGroupRef)
+				particle = GetRefGroupParticle ((XmlSchemaGroupRef)particle);
+
+			if (particle.MaxOccurs > 1) multiValue = true;
+			
+			if (particle is XmlSchemaSequence) {
+				ImportSequenceContent (typeQName, cmap, ((XmlSchemaSequence)particle).Items, classIds, multiValue, ref isMixed);
+			}
+			else if (particle is XmlSchemaChoice) {
+				if (((XmlSchemaChoice)particle).Items.Count == 1)
+					ImportSequenceContent (typeQName, cmap, ((XmlSchemaChoice)particle).Items, classIds, multiValue, ref isMixed);
+				else
+					ImportChoiceContent (typeQName, cmap, (XmlSchemaChoice)particle, classIds, multiValue);
+			}
+			else if (particle is XmlSchemaAll) {
+				ImportSequenceContent (typeQName, cmap, ((XmlSchemaAll)particle).Items, classIds, multiValue, ref isMixed);
+			}
+		}
+
+		void ImportSequenceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaObjectCollection items, CodeIdentifiers classIds, bool multiValue, ref bool isMixed)
+		{
+			foreach (XmlSchemaObject item in items)
+			{
+				XmlTypeMapMember mapMember;
+
+				if (item is XmlSchemaElement)
+				{
+					string ns;
+					XmlSchemaElement elem = (XmlSchemaElement) item;
+					TypeData typeData = GetElementTypeData (typeQName, elem);
+					XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
+
+					if (elem.MaxOccurs == 1 && !multiValue)
+					{
+						XmlTypeMapMemberElement member = null;
+						if (typeData.SchemaType != SchemaTypes.Array)
+						{
+							member = new XmlTypeMapMemberElement ();
+							if (refElem.DefaultValue != null) member.DefaultValue = refElem.DefaultValue;
+						}
+						else if (GetTypeMapping (typeData).IsSimpleType)
+						{
+							// It is a simple list (space separated list).
+							// Since this is not supported, map as a single item value
+							// TODO: improve this
+							member = new XmlTypeMapMemberElement ();
+							typeData = typeData.ListItemTypeData;
+						}
+						else
+							member = new XmlTypeMapMemberList ();
+
+						member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
+						member.TypeData = typeData;
+						member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
+						cmap.AddMember (member);
+					}
+					else
+					{
+						XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
+						member.ListMap = new ListMap ();
+						member.Name = classIds.AddUnique(CodeIdentifier.MakeValid(refElem.Name), member);
+						member.TypeData = typeData.ListTypeData;
+						member.ElementInfo.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
+						member.ListMap.ItemInfo = member.ElementInfo;
+						cmap.AddMember (member);
+					}
+				}
+				else if (item is XmlSchemaAny)
+				{
+					XmlSchemaAny elem = (XmlSchemaAny) item;
+					XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement ();
+					member.Name = classIds.AddUnique ("Any", member);
+
+					Type ctype;
+					if (elem.MaxOccurs > 1 || multiValue)
+						ctype = isMixed ? typeof(XmlNode[]) : typeof(XmlElement[]);
+					else
+						ctype = isMixed ? typeof(XmlNode) : typeof(XmlElement);
+
+					member.TypeData = TypeTranslator.GetTypeData (ctype);
+					XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, member.TypeData);
+					einfo.IsUnnamedAnyElement = true;
+					member.ElementInfo.Add (einfo);
+
+					if (isMixed)
+					{
+						einfo = CreateTextElementInfo (typeQName.Namespace, member, member.TypeData);
+						member.ElementInfo.Add (einfo);
+						member.IsXmlTextCollector = true;
+						isMixed = false;	//Allow only one XmlTextAttribute
+					}
+					
+					cmap.AddMember (member);
+				}
+				else if (item is XmlSchemaParticle) {
+					ImportParticleContent (typeQName, cmap, (XmlSchemaParticle)item, classIds, multiValue, ref isMixed);
+				}
+			}
+		}
+
+		void ImportChoiceContent (XmlQualifiedName typeQName, ClassMap cmap, XmlSchemaChoice choice, CodeIdentifiers classIds, bool multiValue)
+		{
+			XmlTypeMapElementInfoList choices = new XmlTypeMapElementInfoList ();
+			multiValue = ImportChoices (typeQName, null, choices, choice.Items) || multiValue;
+			if (choices.Count == 0) return;
+
+			if (choice.MaxOccurs > 1) multiValue = true;
+
+			XmlTypeMapMemberElement member;
+			if (multiValue)
+			{
+				member = new XmlTypeMapMemberFlatList ();
+				member.Name = classIds.AddUnique ("Items", member);
+				ListMap listMap = new ListMap ();
+				listMap.ItemInfo = choices;
+				((XmlTypeMapMemberFlatList)member).ListMap = listMap;
+			}
+			else
+			{
+				member = new XmlTypeMapMemberElement ();
+				member.Name = classIds.AddUnique ("Item", member);
+			}
+			
+			// If all choices have the same type, use that type for the member.
+			// If not use System.Object.
+			// If there are at least two choices with the same type, use a choice
+			// identifier attribute
+
+			TypeData typeData = null;
+			bool twoEqual = false;
+			bool allEqual = true;
+			Hashtable types = new Hashtable ();
+			foreach (XmlTypeMapElementInfo einfo in choices)
+			{
+				if (types.ContainsKey (einfo.TypeData)) twoEqual = true;
+				else types.Add (einfo.TypeData, einfo);
+
+				TypeData choiceType = einfo.TypeData;
+				if (choiceType.SchemaType == SchemaTypes.Class)
+				{
+					// When comparing class types, use the most generic class in the
+					// inheritance hierarchy
+
+					XmlTypeMapping choiceMap = GetTypeMapping (choiceType);
+					BuildPendingMap (choiceMap);
+					while (choiceMap.BaseMap != null) {
+						choiceMap = choiceMap.BaseMap;
+						BuildPendingMap (choiceMap);
+						choiceType = choiceMap.TypeData;
+					}
+				}
+				
+				if (typeData == null) typeData = choiceType;
+				else if (typeData != choiceType) allEqual = false;
+			}
+
+			if (!allEqual)
+				typeData = TypeTranslator.GetTypeData (typeof(object));
+
+			if (twoEqual)
+			{
+				// Create the choice member
+				XmlTypeMapMemberElement choiceMember = new XmlTypeMapMemberElement ();
+				choiceMember.Name = classIds.AddUnique (member.Name + "ElementName", choiceMember);
+				member.ChoiceMember = choiceMember.Name;
+
+				// Create the choice enum
+				XmlTypeMapping enumMap = CreateTypeMapping (new XmlQualifiedName (member.Name + "ChoiceType", typeQName.Namespace), SchemaTypes.Enum, null);
+				CodeIdentifiers codeIdents = new CodeIdentifiers ();
+				EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [choices.Count];
+				for (int n=0; n<choices.Count; n++)
+				{
+					XmlTypeMapElementInfo it =(XmlTypeMapElementInfo) choices[n];
+					string xmlName = (it.Namespace != null && it.Namespace != "") ? it.Namespace + ":" + it.ElementName : it.ElementName;
+					string enumName = codeIdents.AddUnique (CodeIdentifier.MakeValid (it.ElementName), it);
+					members [n] = new EnumMap.EnumMapMember (xmlName, enumName);
+				}
+				enumMap.ObjectMap = new EnumMap (members, false);
+
+				choiceMember.TypeData = multiValue ? enumMap.TypeData.ListTypeData : enumMap.TypeData;
+				choiceMember.ElementInfo.Add (CreateElementInfo (typeQName.Namespace, choiceMember, choiceMember.Name, choiceMember.TypeData, false));
+				cmap.AddMember (choiceMember);
+			}
+
+			if (multiValue)
+				typeData = typeData.ListTypeData;
+
+			member.ElementInfo = choices;
+			member.TypeData = typeData;
+			cmap.AddMember (member);
+		}
+
+		bool ImportChoices (XmlQualifiedName typeQName, XmlTypeMapMember member, XmlTypeMapElementInfoList choices, XmlSchemaObjectCollection items)
+		{
+			bool multiValue = false;
+			foreach (XmlSchemaObject titem in items)
+			{
+				XmlSchemaObject item = titem;
+				if (item is XmlSchemaGroupRef)
+					item = GetRefGroupParticle ((XmlSchemaGroupRef)item);
+
+				if (item is XmlSchemaElement)
+				{
+					string ns;
+					XmlSchemaElement elem = (XmlSchemaElement) item;
+					TypeData typeData = GetElementTypeData (typeQName, elem);
+					XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
+					choices.Add (CreateElementInfo (ns, member, refElem.Name, typeData, refElem.IsNillable));
+					if (elem.MaxOccurs > 1) multiValue = true;
+				}
+				else if (item is XmlSchemaAny)
+				{
+					XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
+					einfo.IsUnnamedAnyElement = true;
+					choices.Add (einfo);
+				}
+				else if (item is XmlSchemaChoice) {
+					multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaChoice)item).Items) || multiValue;
+				}
+				else if (item is XmlSchemaSequence) {
+					multiValue = ImportChoices (typeQName, member, choices, ((XmlSchemaSequence)item).Items) || multiValue;
+				}
+			}
+			return multiValue;
+		}
+
+		void ImportSimpleContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaSimpleContent content, CodeIdentifiers classIds, bool isMixed)
+		{
+			ClassMap cmap = (ClassMap)map.ObjectMap;
+			
+			XmlQualifiedName qname = GetContentBaseType (content.Content);
+
+			XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
+			member.Name = classIds.AddUnique("Value", member);
+			member.TypeData = FindBuiltInType (qname);
+			member.ElementInfo.Add (CreateTextElementInfo (typeQName.Namespace, member, member.TypeData));
+			member.IsXmlTextCollector = true;
+			cmap.AddMember (member);
+
+			XmlSchemaSimpleContentExtension ext = content.Content as XmlSchemaSimpleContentExtension;
+			if (ext != null)
+				ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
+		}
+
+		TypeData FindBuiltInType (XmlQualifiedName qname)
+		{
+			if (qname.Namespace == XmlSchema.Namespace)
+				return TypeTranslator.GetPrimitiveTypeData (qname.Name);
+
+			XmlSchemaComplexType ct = (XmlSchemaComplexType) schemas.Find (qname, typeof(XmlSchemaComplexType));
+			if (ct != null)
+			{
+				XmlSchemaSimpleContent sc = ct.ContentModel as XmlSchemaSimpleContent;
+				if (sc == null) throw new InvalidOperationException ("Invalid schema");
+				return FindBuiltInType (GetContentBaseType (sc.Content));
+			}
+			
+			XmlSchemaSimpleType st = (XmlSchemaSimpleType) schemas.Find (qname, typeof(XmlSchemaSimpleType));
+			if (st != null)
+				return FindBuiltInType (qname, st);
+
+			throw new InvalidOperationException ("Definition of type " + qname + " not found");
+		}
+
+		TypeData FindBuiltInType (XmlQualifiedName qname, XmlSchemaSimpleType st)
+		{
+			if (CanBeEnum (st))
+				return ImportType (qname, null).TypeData;
+
+			if (st.Content is XmlSchemaSimpleTypeRestriction) {
+				return FindBuiltInType (GetContentBaseType (st.Content));
+			}
+			else if (st.Content is XmlSchemaSimpleTypeList) {
+				return FindBuiltInType (GetContentBaseType (st.Content)).ListTypeData;
+			}
+			else if (st.Content is XmlSchemaSimpleTypeUnion)
+			{
+				// Check if all types of the union are equal. If not, then will use anyType.
+				XmlSchemaSimpleTypeUnion uni = (XmlSchemaSimpleTypeUnion) st.Content;
+				TypeData utype = null;
+
+				// Anonymous types are unique
+				if (uni.BaseTypes.Count != 0 && uni.MemberTypes.Length != 0)
+					return FindBuiltInType (anyType);
+
+				foreach (XmlQualifiedName mt in uni.MemberTypes)
+				{
+					TypeData qn = FindBuiltInType (mt);
+					if (utype != null && qn != utype) return FindBuiltInType (anyType);
+					else utype = qn;
+				}
+				return utype;
+			}
+			else
+				return null;
+		}
+
+		XmlQualifiedName GetContentBaseType (XmlSchemaObject ob)
+		{
+			if (ob is XmlSchemaSimpleContentExtension)
+				return ((XmlSchemaSimpleContentExtension)ob).BaseTypeName;
+			else if (ob is XmlSchemaSimpleContentRestriction)
+				return ((XmlSchemaSimpleContentRestriction)ob).BaseTypeName;
+			else if (ob is XmlSchemaSimpleTypeRestriction)
+				return ((XmlSchemaSimpleTypeRestriction)ob).BaseTypeName;
+			else if (ob is XmlSchemaSimpleTypeList)
+				return ((XmlSchemaSimpleTypeList)ob).ItemTypeName;
+			else
+				return null;
+		}
+
+		void ImportComplexContent (XmlQualifiedName typeQName, XmlTypeMapping map, XmlSchemaComplexContent content, CodeIdentifiers classIds, bool isMixed)
+		{
+			XmlSchemaComplexContentExtension ext = content.Content as XmlSchemaComplexContentExtension;
+
+			ClassMap cmap = (ClassMap)map.ObjectMap;
+			XmlQualifiedName qname;
+
+			if (ext != null) qname = ext.BaseTypeName;
+			else qname = ((XmlSchemaComplexContentRestriction)content.Content).BaseTypeName;
+			
+			// Add base map members to this map
+
+			XmlTypeMapping baseMap = ImportType (qname, null);
+			BuildPendingMap (baseMap);
+			ClassMap baseClassMap = (ClassMap)baseMap.ObjectMap;
+
+			foreach (XmlTypeMapMember member in baseClassMap.AllMembers)
+				cmap.AddMember (member);
+
+			if (baseClassMap.XmlTextCollector != null) isMixed = false;
+			else if (content.IsMixed) isMixed = true;
+
+			map.BaseMap = baseMap;
+			baseMap.DerivedTypes.Add (map);
+
+			if (ext != null) {
+				// Add the members of this map
+				ImportParticleComplexContent (typeQName, cmap, ext.Particle, classIds, isMixed);
+				ImportAttributes (typeQName, cmap, ext.Attributes, ext.AnyAttribute, classIds);
+			}
+			else {
+				if (isMixed) ImportParticleComplexContent (typeQName, cmap, null, classIds, true);
+			}
+		}
+
+		XmlTypeMapping ImportClassSimpleType (XmlQualifiedName typeQName, XmlSchemaSimpleType stype, XmlQualifiedName root)
+		{
+			if (CanBeEnum (stype))
+			{
+				// Create an enum map
+
+				CodeIdentifiers codeIdents = new CodeIdentifiers ();
+				XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
+
+				XmlTypeMapping enumMap = CreateTypeMapping (typeQName, SchemaTypes.Enum, null);
+				codeIdents.AddReserved (enumMap.TypeData.TypeName);
+
+				EnumMap.EnumMapMember[] members = new EnumMap.EnumMapMember [rest.Facets.Count];
+				for (int n=0; n<rest.Facets.Count; n++)
+				{
+					XmlSchemaEnumerationFacet enu = (XmlSchemaEnumerationFacet) rest.Facets[n];
+					string enumName = codeIdents.AddUnique(CodeIdentifier.MakeValid (enu.Value), enu);
+					members [n] = new EnumMap.EnumMapMember (enu.Value, enumName);
+				}
+				enumMap.ObjectMap = new EnumMap (members, false);
+				enumMap.IsSimpleType = true;
+				return enumMap;
+			}
+
+			if (stype.Content is XmlSchemaSimpleTypeList)
+			{
+				XmlSchemaSimpleTypeList slist = (XmlSchemaSimpleTypeList)stype.Content;
+				TypeData arrayTypeData = FindBuiltInType (slist.ItemTypeName, stype);
+
+				ListMap listMap = new ListMap ();
+				listMap.ItemInfo = new XmlTypeMapElementInfoList ();
+				listMap.ItemInfo.Add (CreateElementInfo (typeQName.Namespace, null, "Item", arrayTypeData.ListItemTypeData, false));
+
+				XmlTypeMapping map = CreateArrayTypeMapping (typeQName, arrayTypeData);
+				map.ObjectMap = listMap;
+				map.IsSimpleType = true;
+				return map;
+			}
+
+			// It is an extension of a primitive or known type
+			
+			TypeData typeData = FindBuiltInType (typeQName, stype);
+			return GetTypeMapping (typeData);
+		}
+
+		bool CanBeEnum (XmlSchemaSimpleType stype)
+		{
+			if (stype.Content is XmlSchemaSimpleTypeRestriction)
+			{
+				XmlSchemaSimpleTypeRestriction rest = (XmlSchemaSimpleTypeRestriction)stype.Content;
+				foreach (object ob in rest.Facets)
+					if (!(ob is XmlSchemaEnumerationFacet)) return false;
+				return true;
+			}
+			return false;
+		}
+
+		bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaComplexType stype)
+		{
+			return !stype.IsMixed && CanBeArray (typeQName, stype.Particle, false);
+		}
+
+		bool CanBeArray (XmlQualifiedName typeQName, XmlSchemaParticle particle, bool multiValue)
+		{
+			// To be an array, there can't be a direct child of type typeQName
+
+			if (particle == null) return false;
+
+			multiValue = multiValue || particle.MaxOccurs > 1;
+
+			if (particle is XmlSchemaGroupRef)
+				return CanBeArray (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), multiValue);
+
+			if (particle is XmlSchemaElement)
+			{
+				XmlSchemaElement elem = (XmlSchemaElement)particle;
+				if (!elem.RefName.IsEmpty)
+					return CanBeArray (typeQName, FindRefElement (elem), multiValue);
+				else
+					return multiValue && !typeQName.Equals (((XmlSchemaElement)particle).SchemaTypeName);
+			}
+
+			if (particle is XmlSchemaAny)
+				return multiValue;
+
+			if (particle is XmlSchemaSequence)
+			{
+				XmlSchemaSequence seq = particle as XmlSchemaSequence;
+				if (seq.Items.Count != 1) return false;
+				return CanBeArray (typeQName, (XmlSchemaParticle)seq.Items[0], multiValue);
+			}
+
+			if (particle is XmlSchemaChoice)
+			{
+				// Can be array if all choices have different types
+				ArrayList types = new ArrayList ();
+				if(!CheckChoiceType (typeQName, particle, types, ref multiValue)) return false;
+				return multiValue;
+			}
+
+			return false;
+		}
+
+		bool CheckChoiceType (XmlQualifiedName typeQName, XmlSchemaParticle particle, ArrayList types, ref bool multiValue)
+		{
+			XmlQualifiedName type = null;
+
+			multiValue = multiValue || particle.MaxOccurs > 1;
+
+			if (particle is XmlSchemaGroupRef)
+				return CheckChoiceType (typeQName, GetRefGroupParticle ((XmlSchemaGroupRef)particle), types, ref multiValue);
+
+			if (particle is XmlSchemaElement) {
+				string ns;
+				XmlSchemaElement elem = (XmlSchemaElement)particle;
+				XmlSchemaElement refElem = GetRefElement (typeQName, elem, out ns);
+				if (refElem.SchemaType != null) return true;
+				type = refElem.SchemaTypeName;
+			}
+			else if (particle is XmlSchemaAny) {
+				type = anyType;
+			}
+			else if (particle is XmlSchemaSequence)
+			{
+				XmlSchemaSequence seq = particle as XmlSchemaSequence;
+				foreach (XmlSchemaParticle par in seq.Items)
+					if (!CheckChoiceType (typeQName, par, types, ref multiValue)) return false;
+				return true;
+			}
+			else if (particle is XmlSchemaChoice)
+			{
+				foreach (XmlSchemaParticle choice in ((XmlSchemaChoice)particle).Items)
+					if (!CheckChoiceType (typeQName, choice, types, ref multiValue)) return false;
+				return true;
+			}
+
+			if (typeQName.Equals (type)) return false;
+
+			// For primitive types, compare using CLR types, since several
+			// xml types can be mapped to a single CLR type
+
+			string t;
+			if (type.Namespace == XmlSchema.Namespace)
+				t = TypeTranslator.GetPrimitiveTypeData (type.Name).FullTypeName + ":" + type.Namespace;
+
+			else
+				t = type.Name + ":" + type.Namespace;
+
+			if (types.Contains (t)) return false;
+			types.Add (t);
+			return true;
+		}
+		
+		bool CanBeAnyElement (XmlSchemaComplexType stype)
+		{
+			XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
+			return (seq != null) && (seq.Items.Count == 1) && (seq.Items[0] is XmlSchemaAny);
+		}
+
+		bool CanBeIXmlSerializable (XmlSchemaComplexType stype)
+		{
+			XmlSchemaSequence seq = stype.Particle as XmlSchemaSequence;
+			if (seq == null) return false;
+			if (seq.Items.Count != 2) return false;
+			XmlSchemaElement elem = seq.Items[0] as XmlSchemaElement;
+			if (elem == null) return false;
+			if (elem.RefName != new XmlQualifiedName ("schema",XmlSchema.Namespace)) return false;
+			return (seq.Items[1] is XmlSchemaAny);
+		}
+
+		XmlTypeMapElementInfo CreateElementInfo (string ns, XmlTypeMapMember member, string name, TypeData typeData, bool isNillable)
+		{
+			XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
+			einfo.ElementName = name;
+			einfo.Namespace = ns;
+			einfo.IsNullable = isNillable;
+			if (einfo.TypeData.IsComplexType)
+				einfo.MappedType = GetTypeMapping (typeData);
+			return einfo;
+		}
+
+		XmlTypeMapElementInfo CreateTextElementInfo (string ns, XmlTypeMapMember member, TypeData typeData)
+		{
+			XmlTypeMapElementInfo einfo = new XmlTypeMapElementInfo (member, typeData);
+			einfo.IsTextElement = true;
+			einfo.WrappedElement = false;
+			if (typeData.IsComplexType)
+				einfo.MappedType = GetTypeMapping (typeData);
+			return einfo;
+		}
+
+		XmlTypeMapping CreateTypeMapping (XmlQualifiedName typeQName, SchemaTypes schemaType, XmlQualifiedName root)
+		{
+			string typeName = CodeIdentifier.MakeValid (typeQName.Name);
+			typeName = typeIdentifiers.AddUnique (typeName, null);
+
+			TypeData typeData = new TypeData (typeName, typeName, typeName, schemaType, null);
+
+			XmlQualifiedName elemName = (root != null) ? root : typeQName;
+			
+			XmlTypeMapping map = new XmlTypeMapping (elemName.Name, elemName.Namespace, typeData, typeQName.Name, typeQName.Namespace);
+			mappedTypes [typeQName] = map;
+			dataMappedTypes [typeData] = map;
+
+			return map;
+		}
+
+		XmlTypeMapping CreateArrayTypeMapping (XmlQualifiedName typeQName, TypeData arrayTypeData)
+		{
+			XmlTypeMapping map = new XmlTypeMapping (arrayTypeData.XmlType, typeQName.Namespace, arrayTypeData, arrayTypeData.XmlType, typeQName.Namespace);
+			mappedTypes [typeQName] = map;
+			dataMappedTypes [arrayTypeData] = map;
+
+			return map;
+		}
+
+		XmlSchemaElement GetRefElement (XmlQualifiedName typeQName, XmlSchemaElement elem, out string ns)
+		{
+			if (!elem.RefName.IsEmpty)
+			{
+				ns = elem.RefName.Namespace;
+				return FindRefElement (elem);
+			}
+			else
+			{
+				ns = typeQName.Namespace;
+				return elem;
+			}
+		}
+
+		XmlSchemaAttribute GetRefAttribute (XmlQualifiedName typeQName, XmlSchemaAttribute attr, out string ns)
+		{
+			if (!attr.RefName.IsEmpty)
+			{
+				ns = attr.RefName.Namespace;
+				return (XmlSchemaAttribute) schemas.Find (attr.RefName, typeof(XmlSchemaAttribute));
+			}
+			else
+			{
+				ns = typeQName.Namespace;
+				return attr;
+			}
+		}
+
+		TypeData GetElementTypeData (XmlQualifiedName typeQName, XmlSchemaElement elem)
+		{
+			if (!elem.RefName.IsEmpty) {
+				XmlSchemaElement refElem = FindRefElement (elem);
+				if (refElem == null) throw new InvalidOperationException ("Ref type not found: " + elem.RefName);
+				return GetElementTypeData (typeQName, refElem);
+			}
+			
+			if (!elem.SchemaTypeName.IsEmpty) return GetTypeData (elem.SchemaTypeName);
+			else if (elem.SchemaType == null) return TypeTranslator.GetTypeData (typeof(object));
+			else return GetTypeData (elem.SchemaType, typeQName, elem.Name);
+		}
+
+		TypeData GetAttributeTypeData (XmlQualifiedName typeQName, XmlSchemaAttribute attr)
+		{
+			if (!attr.RefName.IsEmpty)
+				return GetAttributeTypeData (typeQName, (XmlSchemaAttribute)schemas.Find (attr.RefName, typeof (XmlSchemaAttribute)));
+
+			if (!attr.SchemaTypeName.IsEmpty) return GetTypeData (attr.SchemaTypeName);
+			else return GetTypeData (attr.SchemaType, typeQName, attr.Name);
+		}
+
+		TypeData GetTypeData (XmlQualifiedName typeQName)
+		{
+			if (typeQName.Namespace == XmlSchema.Namespace)
+				return TypeTranslator.GetPrimitiveTypeData (typeQName.Name);
+
+			return ImportType (typeQName, null).TypeData;
+		}
+
+		TypeData GetTypeData (XmlSchemaType stype, XmlQualifiedName typeQNname, string propertyName)
+		{
+			string baseName = typeQNname.Name + typeIdentifiers.MakeRightCase (propertyName);
+			baseName = elemIdentifiers.AddUnique (baseName, stype);
+			
+			XmlQualifiedName newName;
+			newName = new XmlQualifiedName (baseName, typeQNname.Namespace);
+
+			XmlTypeMapping map = ImportType (newName, stype, null);
+			return map.TypeData;
+		}
+
+		XmlTypeMapping GetTypeMapping (TypeData typeData)
+		{
+			XmlTypeMapping map = (XmlTypeMapping) dataMappedTypes [typeData];
+			if (map != null) return map;
+			
+			if (map == null && typeData.IsListType)
+			{
+				// Create an array map for the type
+
+				XmlTypeMapping itemMap = GetTypeMapping (typeData.ListItemTypeData);
+				
+				map = new XmlTypeMapping (typeData.XmlType, itemMap.Namespace, typeData, typeData.XmlType, itemMap.Namespace);
+				ListMap listMap = new ListMap ();
+				listMap.ItemInfo = new XmlTypeMapElementInfoList();
+				listMap.ItemInfo.Add (CreateElementInfo (itemMap.Namespace, null, typeData.ListItemTypeData.XmlType, typeData.ListItemTypeData, false));
+				map.ObjectMap = listMap;
+				
+				mappedTypes [new XmlQualifiedName(map.ElementName, map.Namespace)] = map;
+				dataMappedTypes [typeData] = map;
+				return map;
+			}
+			else if (typeData.SchemaType == SchemaTypes.Primitive || typeData.Type == typeof(object) || typeof(XmlNode).IsAssignableFrom(typeData.Type))
+			{
+				map = new XmlTypeMapping (typeData.XmlType, XmlSchema.Namespace, typeData, typeData.XmlType, XmlSchema.Namespace);
+				dataMappedTypes [typeData] = map;
+				return map;
+			}
+			
+			throw new InvalidOperationException ("Map for type " + typeData.TypeName + " not found");
+		}
+
+		XmlTypeMapping GetRegisteredTypeMapping (XmlQualifiedName typeQName)
+		{
+			return (XmlTypeMapping) mappedTypes [typeQName];
+		}
+
+		XmlSchemaParticle GetRefGroupParticle (XmlSchemaGroupRef refGroup)
+		{
+			XmlSchemaGroup grp = (XmlSchemaGroup) schemas.Find (refGroup.RefName, typeof (XmlSchemaGroup));
+			return grp.Particle;
+		}
+
+		XmlSchemaElement FindRefElement (XmlSchemaElement elem)
+		{
+			if (elem.RefName.Namespace == XmlSchema.Namespace)
+			{
+				if (anyElement != null) return anyElement;
+				anyElement = new XmlSchemaElement ();
+				anyElement.Name = "any";
+				anyElement.SchemaTypeName = anyType;
+				return anyElement;
+			}
+			return (XmlSchemaElement) schemas.Find (elem.RefName, typeof(XmlSchemaElement));
 		}
 
 		#endregion // Methods

+ 2 - 6
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriter.cs

@@ -190,12 +190,8 @@ namespace System.Xml.Serialization {
 			string prefix = Writer.LookupPrefix (ns);
 			if (prefix == null) 
 			{
-				if (ns == String.Empty) {
-					prefix = String.Empty;
-				} else {
-					prefix = String.Format ("q{0}", ++qnameCount);
-					WriteAttribute ("xmlns", prefix, null, ns);
-				}
+				prefix = String.Format ("q{0}", ++qnameCount);
+				WriteAttribute ("xmlns", prefix, null, ns);
 			}
 			return prefix;
 		}

+ 1 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlSerializationWriterInterpreter.cs

@@ -299,7 +299,7 @@ namespace System.Xml.Serialization
 			}
 			else {
 				if (typeData.Type == typeof(XmlQualifiedName)) WriteElementQualifiedName (name, ns, (XmlQualifiedName)memberValue);
-				WriteElementString (name, ns, GetStringValue (mappedType, typeData, memberValue));
+				else WriteElementString (name, ns, GetStringValue (mappedType, typeData, memberValue));
 			}
 		}
 

+ 1 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapElementInfo.cs

@@ -64,7 +64,7 @@ namespace System.Xml.Serialization
 			get 
 			{ 
 				if (_mappedType == null) return XmlSchema.Namespace;
-				else return _mappedType.Namespace;
+				else return _mappedType.XmlTypeNamespace;
 			}
 		}
 

+ 1 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMember.cs

@@ -22,7 +22,7 @@ namespace System.Xml.Serialization
 		int _index;
 		TypeData _typeData;
 		MemberInfo _member;
-		object _defaultValue;
+		object _defaultValue = System.DBNull.Value;
 
 		public XmlTypeMapMember()
 		{

+ 5 - 1
mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapMemberElement.cs

@@ -27,7 +27,11 @@ namespace System.Xml.Serialization
 
 		public XmlTypeMapElementInfoList ElementInfo
 		{
-			get { return _elementInfo; }
+			get
+			{
+				if (_elementInfo == null) _elementInfo = new XmlTypeMapElementInfoList ();
+				return _elementInfo;
+			}
 			set { _elementInfo = value; }
 		}
 

+ 24 - 4
mcs/class/System.XML/System.Xml.Serialization/XmlTypeMapping.cs

@@ -19,18 +19,21 @@ namespace System.Xml.Serialization
 		private string elementName;
 		private string ns;
 		private string xmlType;
+		private string xmlTypeNamespace;
 		TypeData type;
 		XmlTypeMapping baseMap;
 		bool multiReferenceType = false;
+		bool isSimpleType;
 
 		ArrayList _derivedTypes = new ArrayList();
 
-		internal XmlTypeMapping(string elementName, string ns, TypeData typeData, string xmlType)
+		internal XmlTypeMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
 		{
 			this.elementName = elementName;
 			this.ns = ns;
 			this.type = typeData;
 			this.xmlType = xmlType;
+			this.xmlTypeNamespace = xmlTypeNamespace;
 		}
 
 		public string ElementName
@@ -63,6 +66,11 @@ namespace System.Xml.Serialization
 			get { return xmlType; }
 		}
 
+		internal string XmlTypeNamespace
+		{
+			get { return xmlTypeNamespace; }
+		}
+
 		internal ArrayList DerivedTypes
 		{
 			get { return _derivedTypes; }
@@ -81,6 +89,12 @@ namespace System.Xml.Serialization
 			set { baseMap = value; }
 		}
 
+		internal bool IsSimpleType
+		{
+			get { return isSimpleType; }
+			set { isSimpleType = value; }
+		}
+
 		internal XmlTypeMapping GetRealTypeMap (string objectFullTypeName)
 		{
 			// Returns the map for a subtype of this map's type
@@ -122,7 +136,10 @@ namespace System.Xml.Serialization
 			{
 				XmlTypeMapMemberAttribute atm = (XmlTypeMapMemberAttribute)member;
 				if (_attributeMembers == null) _attributeMembers = new Hashtable();
-				_attributeMembers.Add (atm.AttributeName + "/" + atm.Namespace, member);
+				string key = atm.AttributeName + "/" + atm.Namespace;
+				if (_attributeMembers.ContainsKey (key))
+					throw new InvalidOperationException ("The XML attribute named '" + atm.AttributeName + "' from namespace '" + atm.Namespace + "' already present in the current scope. Use XML attributes to specify another XML name or namespace for the attribute.");
+				_attributeMembers.Add (key, member);
 				return;
 			}
 			else if (member is XmlTypeMapMemberFlatList)
@@ -221,6 +238,11 @@ namespace System.Xml.Serialization
 			get { return _elementMembers; }
 		}
 
+		public ArrayList AllMembers
+		{
+			get { return _allMembers; }
+		}
+
 		public ICollection FlatLists
 		{
 			get { return _flatLists; }
@@ -279,8 +301,6 @@ namespace System.Xml.Serialization
 			set { _itemInfo = value; }
 		}
 
-
-
 		public XmlTypeMapElementInfo FindElement (object memberValue)
 		{
 			if (_itemInfo.Count == 1)