Merge pull request #103 from XTZGZoReX/master
[mono.git] / mcs / class / System.XML / System.Xml.Serialization / XmlTypeMapping.cs
index da2b006979534571941a9c48c28cb2adbe49c163..752aae73c27d514725370cccbed7de328bfba0b2 100644 (file)
@@ -191,15 +191,15 @@ namespace System.Xml.Serialization
        internal class XmlSerializableMapping : XmlTypeMapping
        {
                XmlSchema _schema;
-#if NET_2_0
+#if NET_2_0 && !MOONLIGHT
                XmlSchemaComplexType _schemaType;
                XmlQualifiedName _schemaTypeName;
 #endif
 
-               internal XmlSerializableMapping(string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
+               internal XmlSerializableMapping(XmlRootAttribute root, string elementName, string ns, TypeData typeData, string xmlType, string xmlTypeNamespace)
                        : base(elementName, ns, typeData, xmlType, xmlTypeNamespace)
                {
-#if NET_2_0
+#if NET_2_0 && !MOONLIGHT
                        XmlSchemaProviderAttribute schemaProvider = (XmlSchemaProviderAttribute) Attribute.GetCustomAttribute (typeData.Type, typeof (XmlSchemaProviderAttribute));
 
                        if (schemaProvider != null) {
@@ -220,7 +220,7 @@ namespace System.Xml.Serialization
 
                                if (retVal is XmlSchemaComplexType) {
                                        _schemaType = (XmlSchemaComplexType) retVal;
-                                       if (xs.Count == 0)
+                                       if (!_schemaType.QualifiedName.IsEmpty)
                                                _schemaTypeName = _schemaType.QualifiedName;
                                        else
                                                _schemaTypeName = new XmlQualifiedName (xmlType, xmlTypeNamespace);
@@ -233,7 +233,7 @@ namespace System.Xml.Serialization
                                                String.Format ("Method {0}.{1}() specified by XmlSchemaProviderAttribute has invalid signature: return type must be compatible with System.Xml.XmlQualifiedName.", typeData.Type.Name, method));
 
                                // defaultNamespace at XmlReflectionImporter takes precedence for Namespace, but not for XsdTypeNamespace.
-                               UpdateRoot (new XmlQualifiedName (_schemaTypeName.Name, Namespace ?? _schemaTypeName.Namespace));
+                               UpdateRoot (new XmlQualifiedName (root != null ? root.ElementName : _schemaTypeName.Name, root != null ? root.Namespace : Namespace ?? _schemaTypeName.Namespace));
                                XmlTypeNamespace = _schemaTypeName.Namespace;
                                XmlType = _schemaTypeName.Name;
 
@@ -246,13 +246,24 @@ namespace System.Xml.Serialization
                                return;
                        }
 #endif
+#if NET_2_0 && !MOONLIGHT
                        IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type, true);
+                       try {
+                               _schema = serializable.GetSchema();
+                       } catch (Exception) {
+                               // LAMESPEC: .NET has a bad exception catch and swallows it silently.
+                       }
+#else
+                       IXmlSerializable serializable = (IXmlSerializable)Activator.CreateInstance (typeData.Type);
                        _schema = serializable.GetSchema();
+#endif
+#if !MOONLIGHT
                        if (_schema != null) 
                        {
                                if (_schema.Id == null || _schema.Id.Length == 0) 
                                        throw new InvalidOperationException("Schema Id is missing. The schema returned from " + typeData.Type.FullName + ".GetSchema() must have an Id.");
                        }
+#endif
                }
 
                internal XmlSchema Schema
@@ -260,7 +271,7 @@ namespace System.Xml.Serialization
                        get { return _schema; }
                }
 
-#if NET_2_0
+#if NET_2_0 && !MOONLIGHT
                internal XmlSchemaType SchemaType {
                        get { return _schemaType; }
                }
@@ -295,7 +306,10 @@ namespace System.Xml.Serialization
 
                public void AddMember (XmlTypeMapMember member)
                {
-                       member.GlobalIndex = _allMembers.Count;
+                       // If GlobalIndex has not been set, set it now
+                       if (member.GlobalIndex == -1)
+                               member.GlobalIndex = _allMembers.Count;
+                       
                        _allMembers.Add (member);
                        
                        if (!(member.DefaultValue is System.DBNull) && member.DefaultValue != null) {