context: // 
  // System.Xml.Serialization.XmlSchemaExporter 
  //
  // Author:
  //   Tim Coleman (tim@timcoleman.com)
  //   Lluis Sanchez Gual (lluis@ximian.com)
  //
  // Copyright (C) Tim Coleman, 2002
  //
  
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  // 
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  // 
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  using System.Xml;
  using System.Xml.Schema;
  using System.Collections;
  
  namespace System.Xml.Serialization {
  	public class XmlSchemaExporter {
  
  		#region Fields
  
  		XmlSchemas schemas;
  		Hashtable exportedMaps = new Hashtable();
  		Hashtable exportedElements = new Hashtable();
  		bool encodedFormat = false;
  		XmlDocument xmlDoc;
  		
  		#endregion
  
  		#region Constructors
  
  		public XmlSchemaExporter (XmlSchemas schemas)
  		{
  			this.schemas = schemas;
  		}
  
  		internal XmlSchemaExporter (XmlSchemas schemas, bool encodedFormat)
  		{
  			this.encodedFormat = encodedFormat;
  			this.schemas = schemas;
  		}
  
  		#endregion // Constructors
  
  		#region Methods
  
  		[MonoTODO]
  		public string ExportAnyType (string ns)
  		{
  			throw new NotImplementedException ();
  		}
  
  #if NET_2_0
  		[MonoNotSupported("")]
  		public string ExportAnyType (XmlMembersMapping members)
  		{
  			throw new NotImplementedException ();
  		}
  #endif
  
  		public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  		{
  			ExportMembersMapping (xmlMembersMapping, true);
  		}
  
  #if NET_2_0
  		public
  #else
  		internal 
  #endif
  		void ExportMembersMapping (XmlMembersMapping xmlMembersMapping, bool exportEnclosingType)
  		{
  			ClassMap cmap = (ClassMap) xmlMembersMapping.ObjectMap;
  
  			if (xmlMembersMapping.HasWrapperElement && exportEnclosingType)
  			{
  				XmlSchema schema = GetSchema (xmlMembersMapping.Namespace);
  				XmlSchemaComplexType stype = new XmlSchemaComplexType ();
  	
  				XmlSchemaSequence particle;
  				XmlSchemaAnyAttribute anyAttribute;
  				ExportMembersMapSchema (schema, cmap, null, stype.Attributes, out particle, out anyAttribute);
  				stype.Particle = particle;
  				stype.AnyAttribute = anyAttribute;
  				
  				if (encodedFormat)
  				{
  					stype.Name = xmlMembersMapping.ElementName;
  					schema.Items.Add (stype);
  				}
  				else
  				{
  					XmlSchemaElement selem = new XmlSchemaElement ();
  					selem.Name = xmlMembersMapping.ElementName;
  					selem.SchemaType = stype;
  					schema.Items.Add (selem);
  				}
  			}
  			else
  			{
  				ICollection members = cmap.ElementMembers;
  				if (members != null)
  				{
  					foreach (XmlTypeMapMemberElement member in members)
  					{
  						if (member is XmlTypeMapMemberAnyElement && member.TypeData.IsListType)
  						{
  							XmlSchema mschema = GetSchema (xmlMembersMapping.Namespace);
  							XmlSchemaParticle par = GetSchemaArrayElement (mschema, member.ElementInfo);
  							if (par is XmlSchemaAny)
  							{
  								XmlSchemaComplexType ct = FindComplexType (mschema.Items, "any");
  								if (ct != null) continue;
  								
  								ct = new XmlSchemaComplexType ();
  								ct.Name = "any";
  								ct.IsMixed = true;
  								XmlSchemaSequence seq = new XmlSchemaSequence ();
  								ct.Particle = seq;
  								seq.Items.Add (par);
  								mschema.Items.Add (ct);
  								continue;
  							}
  						}
  						
  						
  						XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo [0];
  						XmlSchema schema;
  
  						if (encodedFormat)
  						{
  							schema = GetSchema (xmlMembersMapping.Namespace);
  							ImportNamespace (schema, XmlSerializer.EncodingNamespace);
  						}
  						else
  							schema = GetSchema (einfo.Namespace);
  						
  						
  						XmlSchemaElement exe = FindElement (schema.Items, einfo.ElementName);
  						XmlSchemaElement elem;
  						
  						XmlSchemaObjectContainer container = null;
  						// In encoded format, the schema elements are not needed
  						if (!encodedFormat)
  							container = new XmlSchemaObjectContainer (schema);
  
  						Type memType = member.GetType();
  						if (member is XmlTypeMapMemberFlatList)
  							throw new InvalidOperationException ("Unwrapped arrays not supported as parameters");
  						else if (memType == typeof(XmlTypeMapMemberElement))
  							elem = (XmlSchemaElement) GetSchemaElement (schema,
  								einfo, member.DefaultValue, false, container);
  						else
  							elem = (XmlSchemaElement) GetSchemaElement (schema,
  								einfo, false, container);
  						
  						if (exe != null)
  						{
  							if (exe.SchemaTypeName.Equals (elem.SchemaTypeName))
  								schema.Items.Remove (elem);
  							else
  							{
  								string s = "The XML element named '" + einfo.ElementName + "' ";
  								s += "from namespace '" + schema.TargetNamespace + "' references distinct types " + elem.SchemaTypeName.Name + " and " + exe.SchemaTypeName.Name + ". ";
  								s += "Use XML attributes to specify another XML name or namespace for the element or types.";
  								throw new InvalidOperationException (s);
  							}
  						}
  					}
  				}
  			}
  			
  			CompileSchemas ();
  		}
  
  		[MonoTODO]
  		public XmlQualifiedName ExportTypeMapping (XmlMembersMapping xmlMembersMapping)
  		{
  			throw new NotImplementedException ();
  		}
  
  		public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  		{
  			if (!xmlTypeMapping.IncludeInSchema) return;
  			if (IsElementExported (xmlTypeMapping)) return;
  			
  			if (encodedFormat)
  			{
  				ExportClassSchema (xmlTypeMapping);
  
gt: 				XmlSchema schema = GetSchema (xmlTypeMapping.XmlTypeNamespace);