System.Xml.Serialization.SerializationCodeGenerator.GenerateReadListElement C# (CSharp) Method

GenerateReadListElement() private method

private GenerateReadListElement ( XmlTypeMapping typeMap, string list, string isNullable, bool canCreateInstance ) : string
typeMap XmlTypeMapping
list string
isNullable string
canCreateInstance bool
return string
		string GenerateReadListElement (XmlTypeMapping typeMap, string list, string isNullable, bool canCreateInstance)
		{
			Type listType = typeMap.TypeData.Type;
			ListMap listMap = (ListMap)typeMap.ObjectMap;
			bool doNullCheck = typeMap.TypeData.Type.IsArray;

			if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) 
			{
				if (list == null) {
					list = GetObTempVar ();
					WriteLine (typeMap.TypeData.CSharpFullName + " " + list + " = null;");
					if (doNullCheck)
						WriteLineInd ("if (!ReadNull()) {");
					WriteLine (list + " = " + GenerateCreateList (listType) + ";");
				} else {
					if (doNullCheck)
						WriteLineInd ("if (!ReadNull()) {");
				}
			}
			else
			{
				if (list != null) {
					WriteLineInd ("if (((object)" + list + ") == null)");
					WriteLine ("throw CreateReadOnlyCollectionException (" + GetLiteral (typeMap.TypeData.CSharpFullName) + ");");
					Unindent ();
					doNullCheck = false;
				}
				else {
					WriteLine ("throw CreateReadOnlyCollectionException (" + GetLiteral (typeMap.TypeData.CSharpFullName) + ");");
					return list;
				}
			}
				
			WriteLineInd ("if (Reader.IsEmptyElement) {");
			WriteLine ("Reader.Skip();");
			if (listType.IsArray)
				WriteLine (list + " = (" + typeMap.TypeData.CSharpFullName + ") ShrinkArray (" + list + ", 0, " + GetTypeOf(listType.GetElementType()) + ", false);");

			Unindent ();
			WriteLineInd ("} else {");

			string index = GetNumTempVar ();
			WriteLine ("int " + index + " = 0;");
			WriteLine ("Reader.ReadStartElement();");
			WriteLine ("Reader.MoveToContent();");
			WriteLine ("");

			WriteLine ("while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) ");
			WriteLineInd ("{");
			WriteLine ("if (Reader.NodeType == System.Xml.XmlNodeType.Element) ");
			WriteLineInd ("{");

			bool first = true;
			foreach (XmlTypeMapElementInfo elemInfo in listMap.ItemInfo)
			{
				WriteLineInd ((first?"":"else ") + "if (Reader.LocalName == " + GetLiteral (elemInfo.ElementName) + " && Reader.NamespaceURI == " + GetLiteral (elemInfo.Namespace) + ") {");
				GenerateAddListValue (typeMap.TypeData, list, index, GenerateReadObjectElement (elemInfo), false);
				WriteLine (index + "++;");
				WriteLineUni ("}");
				first = false;
			}
			if (!first) WriteLine ("else UnknownNode (null);");
			else WriteLine ("UnknownNode (null);");
			
			WriteLineUni ("}");
			WriteLine ("else UnknownNode (null);");
			WriteLine ("");
			WriteLine ("Reader.MoveToContent();");
			WriteLineUni ("}");
			
			WriteLine ("ReadEndElement();");

			if (listType.IsArray)
				WriteLine (list + " = (" + typeMap.TypeData.CSharpFullName + ") ShrinkArray (" + list + ", " + index + ", " + GetTypeOf(listType.GetElementType()) + ", false);");

			WriteLineUni ("}");
			if (doNullCheck)
				WriteLineUni ("}");

			return list;
		}
SerializationCodeGenerator