void WriteListContent(object container, TypeData listType, ListMap map, object ob, StringBuilder targetString) { if (listType.Type.IsArray) { Array array = (Array)ob; for (int n = 0; n < array.Length; n++) { object item = array.GetValue(n); XmlTypeMapElementInfo info = map.FindElement(container, n, item); if (info != null && targetString == null) { WriteMemberElement(info, item); } else if (info != null && targetString != null) { targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" "); } else if (item != null) { throw CreateUnknownTypeException(item); } } } else if (ob is ICollection) { int count = (int)ob.GetType().GetProperty("Count").GetValue(ob, null); PropertyInfo itemProp = TypeData.GetIndexerProperty(listType.Type); object[] index = new object[1]; for (int n = 0; n < count; n++) { index[0] = n; object item = itemProp.GetValue(ob, index); XmlTypeMapElementInfo info = map.FindElement(container, n, item); if (info != null && targetString == null) { WriteMemberElement(info, item); } else if (info != null && targetString != null) { targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" "); } else if (item != null) { throw CreateUnknownTypeException(item); } } } else if (ob is IEnumerable) { IEnumerable e = (IEnumerable)ob; foreach (object item in e) { XmlTypeMapElementInfo info = map.FindElement(container, -1, item); if (info != null && targetString == null) { WriteMemberElement(info, item); } else if (info != null && targetString != null) { targetString.Append(GetStringValue(info.MappedType, info.TypeData, item)).Append(" "); } else if (item != null) { throw CreateUnknownTypeException(item); } } } else { throw new Exception("Unsupported collection type"); } }
object ReadListElement(XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance) { Type listType = typeMap.TypeData.Type; ListMap listMap = (ListMap)typeMap.ObjectMap; if (listType.IsArray && ReadNull()) { return(null); } if (list == null) { if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) { list = CreateList(listType); } else { throw CreateReadOnlyCollectionException(typeMap.TypeFullName); } } if (Reader.IsEmptyElement) { Reader.Skip(); if (listType.IsArray) { list = ShrinkArray((Array)list, 0, listType.GetElementType(), false); } return(list); } int index = 0; Reader.ReadStartElement(); Reader.MoveToContent(); while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) { if (Reader.NodeType == System.Xml.XmlNodeType.Element) { XmlTypeMapElementInfo elemInfo = listMap.FindElement(Reader.LocalName, Reader.NamespaceURI); if (elemInfo != null) { AddListValue(typeMap.TypeData, ref list, index++, ReadObjectElement(elemInfo), false); } else { UnknownNode(null); } } else { UnknownNode(null); } Reader.MoveToContent(); } ReadEndElement(); if (listType.IsArray) { list = ShrinkArray((Array)list, index, listType.GetElementType(), false); } return(list); }