void GenerateWriteObject (XmlTypeMapping typeMap)
{
WriteLine ("void " + GetWriteObjectName (typeMap) + " (" + typeMap.TypeData.CSharpFullName + " ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)");
WriteLineInd ("{");
PushHookContext ();
SetHookVar ("$TYPE", typeMap.TypeData.CSharpName);
SetHookVar ("$FULLTYPE", typeMap.TypeData.CSharpFullName);
SetHookVar ("$OBJECT", "ob");
SetHookVar ("$NULLABLE", "isNullable");
if (GenerateWriteHook (HookType.type, typeMap.TypeData.Type))
{
WriteLineUni ("}");
WriteLine ("");
PopHookContext ();
return;
}
if (!typeMap.TypeData.IsValueType)
{
WriteLine ("if (((object)ob) == null)");
WriteLineInd ("{");
WriteLineInd ("if (isNullable)");
if (_format == SerializationFormat.Literal)
WriteLine ("WriteNullTagLiteral(element, namesp);");
else
WriteLine ("WriteNullTagEncoded (element, namesp);");
WriteLineUni ("return;");
WriteLineUni ("}");
WriteLine ("");
}
if (typeMap.TypeData.SchemaType == SchemaTypes.XmlNode)
{
if (_format == SerializationFormat.Literal)
WriteLine ("WriteElementLiteral (ob, \"\", \"\", true, false);");
else
WriteLine ("WriteElementEncoded (ob, \"\", \"\", true, false);");
GenerateEndHook ();
WriteLineUni ("}");
WriteLine ("");
PopHookContext ();
return;
}
if (typeMap.TypeData.SchemaType == SchemaTypes.XmlSerializable)
{
WriteLine ("WriteSerializable (ob, element, namesp, isNullable);");
GenerateEndHook ();
WriteLineUni ("}");
WriteLine ("");
PopHookContext ();
return;
}
ArrayList types = typeMap.DerivedTypes;
WriteLine ("System.Type type = ob.GetType ();");
WriteLine ("if (type == typeof(" + typeMap.TypeData.CSharpFullName + "))");
WriteLine ("{ }");
for (int n=0; n<types.Count; n++)
{
XmlTypeMapping map = (XmlTypeMapping)types[n];
WriteLineInd ("else if (type == typeof(" + map.TypeData.CSharpFullName + ")) { ");
WriteLine (GetWriteObjectName (map) + "((" + map.TypeData.CSharpFullName + ")ob, element, namesp, isNullable, true, writeWrappingElem);");
WriteLine ("return;");
WriteLineUni ("}");
}
if (typeMap.TypeData.Type == typeof (object)) {
WriteLineInd ("else {");
WriteLineInd ("if (ob.GetType().IsArray && typeof(XmlNode).IsAssignableFrom(ob.GetType().GetElementType())) {");
WriteLine ("Writer.WriteStartElement (" + GetLiteral (typeMap.ElementName) + ", " + GetLiteral (typeMap.Namespace) + ");");
WriteLineInd ("foreach (XmlNode node in (System.Collections.IEnumerable) ob)");
WriteLineUni ("node.WriteTo (Writer);");
WriteLineUni ("Writer.WriteEndElement ();");
WriteLine ("}");
WriteLineInd ("else");
WriteLineUni ("WriteTypedPrimitive (element, namesp, ob, true);");
WriteLine ("return;");
WriteLineUni ("}");
}
else
{
WriteLineInd ("else {");
WriteLine ("throw CreateUnknownTypeException (ob);");
WriteLineUni ("}");
WriteLine ("");
WriteLineInd ("if (writeWrappingElem) {");
if (_format == SerializationFormat.Encoded) WriteLine ("needType = true;");
WriteLine ("WriteStartElement (element, namesp, ob);");
WriteLineUni ("}");
WriteLine ("");
WriteLine ("if (needType) WriteXsiType(" + GetLiteral(typeMap.XmlType) + ", " + GetLiteral(typeMap.XmlTypeNamespace) + ");");
WriteLine ("");
switch (typeMap.TypeData.SchemaType)
{
case SchemaTypes.Class: GenerateWriteObjectElement (typeMap, "ob", false); break;
case SchemaTypes.Array: GenerateWriteListElement (typeMap, "ob"); break;
case SchemaTypes.Primitive: GenerateWritePrimitiveElement (typeMap, "ob"); break;
case SchemaTypes.Enum: GenerateWriteEnumElement (typeMap, "ob"); break;
}
WriteLine ("if (writeWrappingElem) WriteEndElement (ob);");
}
GenerateEndHook ();
WriteLineUni ("}");
WriteLine ("");
PopHookContext ();
}