context: using System;
  using System.Xml;
  using System.Xml.Schema;
  using System.Xml.Serialization;
  using System.Text;
  using System.Collections;
  using System.Globalization;
  
  namespace System.Xml.Schema
  {
  
  	internal class XmlSchemaSerializationWriter : XmlSerializationWriter
  	{
  		const string xmlNamespace = "http://www.w3.org/2000/xmlns/";
  		public void WriteRoot_XmlSchema (object o)
  		{
  			WriteStartDocument ();
  			System.Xml.Schema.XmlSchema ob = (System.Xml.Schema.XmlSchema) o;
  			TopLevelElement ();
  			WriteObject_XmlSchema (ob, "schema", "http://www.w3.org/2001/XMLSchema", true, false, true);
  		}
  
  		void WriteObject_XmlSchema (System.Xml.Schema.XmlSchema ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
  		{
  			if (ob == null)
  			{
  				if (isNullable)
  					WriteNullTagLiteral(element, namesp);
  				return;
  			}
  
  			System.Type type = ob.GetType ();
  			if (type == typeof(System.Xml.Schema.XmlSchema))
  				;
  			else {
  				throw CreateUnknownTypeException (ob);
  			}
  
  			if (writeWrappingElem) {
  				WriteStartElement (element, namesp, ob);
  			}
  
  			if (needType) WriteXsiType("XmlSchema", "http://www.w3.org/2001/XMLSchema");
  
  			WriteNamespaceDeclarations ((XmlSerializerNamespaces) ob.@Namespaces);
  
  			ICollection o0 = ob.@UnhandledAttributes;
  			if (o0 != null) {
  				foreach (XmlAttribute o1 in o0)
  					if (o1.NamespaceURI != xmlNamespace)
  						WriteXmlAttribute (o1, ob);
  			}
  
  			if (ob.@AttributeFormDefault != ((System.Xml.Schema.XmlSchemaForm) System.Xml.Schema.XmlSchemaForm.None)) {
  				WriteAttribute ("attributeFormDefault", "", GetEnumValue_XmlSchemaForm (ob.@AttributeFormDefault));
  			}
  			if (ob.@BlockDefault != ((System.Xml.Schema.XmlSchemaDerivationMethod) System.Xml.Schema.XmlSchemaDerivationMethod.None)) {
  				WriteAttribute ("blockDefault", "", GetEnumValue_XmlSchemaDerivationMethod (ob.@BlockDefault));
  			}
  			if (ob.@FinalDefault != ((System.Xml.Schema.XmlSchemaDerivationMethod) System.Xml.Schema.XmlSchemaDerivationMethod.None)) {
  				WriteAttribute ("finalDefault", "", GetEnumValue_XmlSchemaDerivationMethod (ob.@FinalDefault));
  			}
  			if (ob.@ElementFormDefault != ((System.Xml.Schema.XmlSchemaForm) System.Xml.Schema.XmlSchemaForm.None)) {
  				WriteAttribute ("elementFormDefault", "", GetEnumValue_XmlSchemaForm (ob.@ElementFormDefault));
  			}
  			WriteAttribute ("targetNamespace", "", ob.@TargetNamespace);
  			WriteAttribute ("version", "", ob.@Version);
  			WriteAttribute ("id", "", ob.@Id);
  
  			if (ob.@Includes != null) {
  				for (int n2 = 0; n2 < ob.@Includes.Count; n2++) {
  					if (ob.@Includes[n2] == null) { }
  					else if (ob.@Includes[n2].GetType() == typeof(System.Xml.Schema.XmlSchemaInclude)) {
  						WriteObject_XmlSchemaInclude (((System.Xml.Schema.XmlSchemaInclude) ob.@Includes[n2]), "include", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Includes[n2].GetType() == typeof(System.Xml.Schema.XmlSchemaImport)) {
  						WriteObject_XmlSchemaImport (((System.Xml.Schema.XmlSchemaImport) ob.@Includes[n2]), "import", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Includes[n2].GetType() == typeof(System.Xml.Schema.XmlSchemaRedefine)) {
  						WriteObject_XmlSchemaRedefine (((System.Xml.Schema.XmlSchemaRedefine) ob.@Includes[n2]), "redefine", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else throw CreateUnknownTypeException (ob.@Includes[n2]);
  				}
  			}
  			if (ob.@Items != null) {
  				for (int n3 = 0; n3 < ob.@Items.Count; n3++) {
  					if (ob.@Items[n3] == null) { }
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaElement)) {
  						WriteObject_XmlSchemaElement (((System.Xml.Schema.XmlSchemaElement) ob.@Items[n3]), "element", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaSimpleType)) {
  						WriteObject_XmlSchemaSimpleType (((System.Xml.Schema.XmlSchemaSimpleType) ob.@Items[n3]), "simpleType", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaAttribute)) {
  						WriteObject_XmlSchemaAttribute (((System.Xml.Schema.XmlSchemaAttribute) ob.@Items[n3]), "attribute", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaAnnotation)) {
  						WriteObject_XmlSchemaAnnotation (((System.Xml.Schema.XmlSchemaAnnotation) ob.@Items[n3]), "annotation", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaAttributeGroup)) {
  						WriteObject_XmlSchemaAttributeGroup (((System.Xml.Schema.XmlSchemaAttributeGroup) ob.@Items[n3]), "attributeGroup", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaGroup)) {
  						WriteObject_XmlSchemaGroup (((System.Xml.Schema.XmlSchemaGroup) ob.@Items[n3]), "group", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaComplexType)) {
  						WriteObject_XmlSchemaComplexType (((System.Xml.Schema.XmlSchemaComplexType) ob.@Items[n3]), "complexType", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else if (ob.@Items[n3].GetType() == typeof(System.Xml.Schema.XmlSchemaNotation)) {
  						WriteObject_XmlSchemaNotation (((System.Xml.Schema.XmlSchemaNotation) ob.@Items[n3]), "notation", "http://www.w3.org/2001/XMLSchema", false, false, true);
  					}
  					else throw CreateUnknownTypeException (ob.@Items[n3]);
  				}
  			}
  			if (writeWrappingElem) WriteEndElement (ob);
  		}
  /*
  		void WriteObject_XmlSchemaForm (System.Xml.Schema.XmlSchemaForm ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
  		{
  			System.Type type = ob.GetType ();
  			if (type == typeof(System.Xml.Schema.XmlSchemaForm))
  				;
  			else {
  				throw CreateUnknownTypeException (ob);
  			}
  
  			if (writeWrappingElem) {
  				WriteStartElement (element, namesp, ob);
  			}
  
  			if (needType) WriteXsiType("XmlSchemaForm", "");
  
  			Writer.WriteString (GetEnumValue_XmlSchemaForm (ob));
  			if (writeWrappingElem) WriteEndElement (ob);
  		}
  */
  		string GetEnumValue_XmlSchemaForm (System.Xml.Schema.XmlSchemaForm val)
  		{
  			switch (val)
  			{
  				case System.Xml.Schema.XmlSchemaForm.Qualified: return "qualified";
  				case System.Xml.Schema.XmlSchemaForm.Unqualified: return "unqualified";
  				default: return ((long)val).ToString(CultureInfo.InvariantCulture);
  			}
  		}
  /*
  		void WriteObject_XmlSchemaDerivationMethod (System.Xml.Schema.XmlSchemaDerivationMethod ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
  		{
  			System.Type type = ob.GetType ();
  			if (type == typeof(System.Xml.Schema.XmlSchemaDerivationMethod))
  				;
  			else {
  				throw CreateUnknownTypeException (ob);
  			}
  
  			if (writeWrappingElem) {
  				WriteStartElement (element, namesp, ob);
  			}
  
  			if (needType) WriteXsiType("XmlSchemaDerivationMethod", "");
  
  			Writer.WriteString (GetEnumValue_XmlSchemaDerivationMethod (ob));
  			if (writeWrappingElem) WriteEndElement (ob);
  		}
  */
  		string GetEnumValue_XmlSchemaDerivationMethod (System.Xml.Schema.XmlSchemaDerivationMethod val)
  		{
  			switch (val)
  			{
  				case System.Xml.Schema.XmlSchemaDerivationMethod.Empty: return "";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.Substitution: return "substitution";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.Extension: return "extension";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.Restriction: return "restriction";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.List: return "list";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.Union: return "union";
  				case System.Xml.Schema.XmlSchemaDerivationMethod.All: return "#all";
  				default:
  					System.Text.StringBuilder sb = new System.Text.StringBuilder ();
  					string[] enumNames = val.ToString().Split (',');
  					foreach (string name in enumNames)
  					{
  						switch (name.Trim())
  						{
  							case "Empty": sb.Append ("").Append (' '); break; 
  							case "Substitution": sb.Append ("substitution").Append (' '); break; 
  							case "Extension": sb.Append ("extension").Append (' '); break; 
  							case "Restriction": sb.Append ("restriction").Append (' '); break; 
  							case "List": sb.Append ("list").Append (' '); break; 
  							case "Union": sb.Append ("union").Append (' '); break; 
  							case "All": sb.Append ("#all").Append (' '); break; 
  							default: sb.Append (name.Trim()).Append (' '); break; 
  						}
  					}
  					return sb.ToString ().Trim();
  			}
  		}
  
  		void WriteObject_XmlSchemaInclude (System.Xml.Schema.XmlSchemaInclude ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
  		{
  			if (ob == null)
  			{
  				if (isNullable)
  					WriteNullTagLiteral(element, namesp);
  				return;
  			}
  
  			System.Type type = ob.GetType ();
  			if (type == typeof(System.Xml.Schema.XmlSchemaInclude))
  				;
  			else {
  				throw CreateUnknownTypeException (ob);
  			}
  
  			if (writeWrappingElem) {
  				WriteStartElement (element, namesp, ob);
  			}
  
  			if (needType) WriteXsiType("XmlSchemaInclude", "http://www.w3.org/2001/XMLSchema");
  
  			WriteNamespaceDeclarations ((XmlSerializerNamespaces) ob.@Namespaces);
  
  			ICollection o4 = ob.@UnhandledAttributes;
  			if (o4 != null) {
  				foreach (XmlAttribute o5 in o4)
  					if (o5.NamespaceURI != xmlNamespace)
  						WriteXmlAttribute (o5, ob);
  			}
  
  			WriteAttribute ("schemaLocation", "", ob.@SchemaLocation);
  			WriteAttribute ("id", "", ob.@Id);
  
  			WriteObject_XmlSchemaAnnotation (ob.@Annotation, "annotation", "http://www.w3.org/2001/XMLSchema", false, false, true);
  			if (writeWrappingElem) WriteEndElement (ob);
  		}
  
  		void WriteObject_XmlSchemaImport (System.Xml.Schema.XmlSchemaImport ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
  		{
  			if (ob == null)
  			{
  				if (isNullable)
  					WriteNullTagLiteral(element, namesp);
  				return;
  			}
  
  			System.Type type = ob.GetType ();
  			if (type == typeof(System.Xml.Schema.XmlSchemaImport))
  				;
  			else {
  				throw CreateUnknownTypeException (ob);
  			}
  
  
gt: 			if (writeWrappingElem) {