2 // XmlSerializationReaderInterpreter.cs:
5 // Lluis Sanchez Gual (lluis@ximian.com)
7 // (C) 2002, 2003 Ximian, Inc. http://www.ximian.com
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Collections;
35 namespace System.Xml.Serialization
37 internal class XmlSerializationReaderInterpreter: XmlSerializationReader
40 SerializationFormat _format;
41 static readonly XmlQualifiedName AnyType = new XmlQualifiedName("anyType", System.Xml.Schema.XmlSchema.Namespace);
43 public XmlSerializationReaderInterpreter(XmlMapping typeMap)
46 _format = typeMap.Format;
49 protected override void InitCallbacks ()
51 ArrayList maps = _typeMap.RelatedMaps;
54 foreach (XmlTypeMapping map in maps)
56 if (map.TypeData.SchemaType == SchemaTypes.Class || map.TypeData.SchemaType == SchemaTypes.Enum)
58 ReaderCallbackInfo info = new ReaderCallbackInfo (this, map);
59 AddReadCallback (map.XmlType, map.Namespace, map.TypeData.Type, new XmlSerializationReadCallback (info.ReadObject));
65 protected override void InitIDs ()
69 protected XmlTypeMapping GetTypeMap (Type type)
71 ArrayList maps = _typeMap.RelatedMaps;
74 foreach (XmlTypeMapping map in maps)
75 if (map.TypeData.Type == type) return map;
77 throw new InvalidOperationException ("Type " + type + " not mapped");
80 public object ReadRoot ()
82 Reader.MoveToContent();
83 if (_typeMap is XmlTypeMapping)
85 if (_format == SerializationFormat.Literal)
86 return ReadRoot ((XmlTypeMapping)_typeMap);
88 return ReadEncodedObject ((XmlTypeMapping)_typeMap);
91 return ReadMessage ((XmlMembersMapping)_typeMap);
94 object ReadEncodedObject (XmlTypeMapping typeMap)
97 Reader.MoveToContent();
98 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
100 if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace)
101 ob = ReadReferencedElement();
103 throw CreateUnknownNodeException();
108 ReadReferencedElements();
112 protected virtual object ReadMessage (XmlMembersMapping typeMap)
114 object[] parameters = new object[typeMap.Count];
116 if (typeMap.HasWrapperElement)
118 if (_format == SerializationFormat.Encoded)
120 while (Reader.NodeType == System.Xml.XmlNodeType.Element)
122 string root = Reader.GetAttribute ("root", XmlSerializer.EncodingNamespace);
123 if (root == null || System.Xml.XmlConvert.ToBoolean(root)) break;
124 ReadReferencedElement ();
125 Reader.MoveToContent ();
129 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
131 if (Reader.IsStartElement(typeMap.ElementName, typeMap.Namespace)
132 || _format == SerializationFormat.Encoded)
134 if (Reader.IsEmptyElement) { Reader.Skip(); Reader.MoveToContent(); continue; }
135 Reader.ReadStartElement();
136 ReadMembers ((ClassMap)typeMap.ObjectMap, parameters, true, false);
143 Reader.MoveToContent();
147 ReadMembers ((ClassMap)typeMap.ObjectMap, parameters, true, _format == SerializationFormat.Encoded);
149 if (_format == SerializationFormat.Encoded)
150 ReadReferencedElements();
155 object ReadRoot (XmlTypeMapping rootMap)
157 if (rootMap.TypeData.SchemaType == SchemaTypes.XmlNode)
159 return ReadXmlNodeElement (rootMap, true);
163 if (Reader.LocalName != rootMap.ElementName || Reader.NamespaceURI != rootMap.Namespace)
164 throw CreateUnknownNodeException();
166 return ReadObject (rootMap, rootMap.IsNullable, true);
171 protected virtual object ReadObject (XmlTypeMapping typeMap, bool isNullable, bool checkType)
173 switch (typeMap.TypeData.SchemaType)
175 case SchemaTypes.Class: return ReadClassInstance (typeMap, isNullable, checkType);
176 case SchemaTypes.Array: return ReadListElement (typeMap, isNullable, null, true);
177 case SchemaTypes.XmlNode: return ReadXmlNodeElement (typeMap, isNullable);
178 case SchemaTypes.Primitive: return ReadPrimitiveElement (typeMap, isNullable);
179 case SchemaTypes.Enum: return ReadEnumElement (typeMap, isNullable);
180 case SchemaTypes.XmlSerializable: return ReadXmlSerializableElement (typeMap, isNullable);
181 default: throw new Exception ("Unsupported map type");
185 protected virtual object ReadClassInstance (XmlTypeMapping typeMap, bool isNullable, bool checkType)
187 if (isNullable && ReadNull()) return null;
191 System.Xml.XmlQualifiedName t = GetXsiType();
194 XmlTypeMapping realMap = typeMap.GetRealElementMap (t.Name, t.Namespace);
195 if (realMap == null) {
196 if (typeMap.TypeData.Type == typeof(object))
197 return ReadTypedPrimitive (t);
199 throw CreateUnknownTypeException((System.Xml.XmlQualifiedName)t);
201 if (realMap != typeMap)
202 return ReadObject (realMap, false, false);
204 else if (typeMap.TypeData.Type == typeof(object))
205 return ReadTypedPrimitive (AnyType);
208 object ob = Activator.CreateInstance (typeMap.TypeData.Type);
210 Reader.MoveToElement();
211 bool isEmpty = Reader.IsEmptyElement;
212 ReadClassInstanceMembers (typeMap, ob);
214 if (isEmpty) Reader.Skip();
215 else ReadEndElement();
220 protected virtual void ReadClassInstanceMembers (XmlTypeMapping typeMap, object ob)
222 ReadMembers ((ClassMap) typeMap.ObjectMap, ob, false, false);
225 void ReadMembers (ClassMap map, object ob, bool isValueList, bool readByOrder)
227 // Set the default values of the members
228 if (map.MembersWithDefault != null)
230 ArrayList members = map.MembersWithDefault;
231 for (int n=0; n<members.Count; n++) {
232 XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
233 SetMemberValueFromAttr (mem, ob, mem.DefaultValue, isValueList);
239 XmlTypeMapMember anyAttrMember = map.DefaultAnyAttributeMember;
240 int anyAttributeIndex = 0;
241 object anyAttributeArray = null;
243 while (Reader.MoveToNextAttribute())
245 XmlTypeMapMemberAttribute member = map.GetAttribute (Reader.LocalName, Reader.NamespaceURI);
249 SetMemberValue (member, ob, GetValueFromXmlString (Reader.Value, member.TypeData, member.MappedType), isValueList);
251 else if (IsXmlnsAttribute(Reader.Name))
253 // If the map has NamespaceDeclarations,
254 // then store this xmlns to the given member.
255 // If the instance doesn't exist, then create.
256 if (map.NamespaceDeclarations != null) {
257 XmlSerializerNamespaces nss = this.GetMemberValue (map.NamespaceDeclarations, ob, isValueList) as XmlSerializerNamespaces;
259 nss = new XmlSerializerNamespaces ();
260 SetMemberValue (map.NamespaceDeclarations, ob, nss, isValueList);
262 if (Reader.Prefix == "xmlns")
263 nss.Add (Reader.LocalName, Reader.Value);
265 nss.Add ("", Reader.Value);
268 else if (anyAttrMember != null)
270 XmlAttribute attr = (XmlAttribute) Document.ReadNode(Reader);
271 ParseWsdlArrayType (attr);
272 AddListValue (anyAttrMember.TypeData, ref anyAttributeArray, anyAttributeIndex++, attr, true);
275 ProcessUnknownAttribute(ob);
278 if (anyAttrMember != null)
280 anyAttributeArray = ShrinkArray ((Array)anyAttributeArray, anyAttributeIndex, anyAttrMember.TypeData.Type.GetElementType(), true);
281 SetMemberValue (anyAttrMember, ob, anyAttributeArray, isValueList);
286 Reader.MoveToElement();
287 if (Reader.IsEmptyElement) {
288 SetListMembersDefaults (map, ob, isValueList);
292 Reader.ReadStartElement();
297 bool[] readFlag = new bool[(map.ElementMembers != null) ? map.ElementMembers.Count : 0];
299 bool hasAnyReturnMember = (isValueList && _format == SerializationFormat.Encoded && map.ReturnMember != null);
301 Reader.MoveToContent();
303 int[] indexes = null;
304 object[] flatLists = null;
305 object[] flatListsChoices = null;
311 if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
315 maxInd = int.MaxValue;
317 if (map.FlatLists != null)
319 indexes = new int[map.FlatLists.Count];
320 flatLists = new object[map.FlatLists.Count];
321 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists) {
322 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
323 flatLists [mem.FlatArrayIndex] = mem.GetValue (ob);
324 else if (mem.TypeData.Type.IsArray) {
325 flatLists [mem.FlatArrayIndex] = InitializeList (mem.TypeData);
328 object list = mem.GetValue (ob);
330 list = InitializeList (mem.TypeData);
331 SetMemberValue (mem, ob, list, isValueList);
333 flatLists [mem.FlatArrayIndex] = list;
336 if (mem.ChoiceMember != null) {
337 if (flatListsChoices == null)
338 flatListsChoices = new object [map.FlatLists.Count];
339 flatListsChoices [mem.FlatArrayIndex] = InitializeList (mem.ChoiceTypeData);
344 if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
346 FixupCallbackInfo info = new FixupCallbackInfo (this, map, isValueList);
347 fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count);
351 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd))
353 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
355 XmlTypeMapElementInfo info;
358 info = map.GetElement (ind++);
360 else if (hasAnyReturnMember) {
361 info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0];
362 hasAnyReturnMember = false;
365 info = map.GetElement (Reader.LocalName, Reader.NamespaceURI);
367 if (info != null && !readFlag[info.Member.Index] )
369 if (info.Member.GetType() == typeof (XmlTypeMapMemberList))
371 if (_format == SerializationFormat.Encoded && info.MultiReferenceType)
373 object list = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
374 if (fixup.Ids[info.Member.Index] == null) // Already read
376 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) throw CreateReadOnlyCollectionException (info.TypeData.FullTypeName);
377 else SetMemberValue (info.Member, ob, list, isValueList);
379 else if (!info.MappedType.TypeData.Type.IsArray)
381 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList))
382 list = GetMemberValue (info.Member, ob, isValueList);
384 list = CreateList (info.MappedType.TypeData.Type);
385 SetMemberValue (info.Member, ob, list, isValueList);
387 AddFixup (new CollectionFixup (list, new XmlSerializationCollectionFixupCallback (FillList), fixup.Ids[info.Member.Index]));
388 fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed.
393 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) {
394 ReadListElement (info.MappedType, info.IsNullable, GetMemberValue (info.Member, ob, isValueList), false);
395 } else if (info.MappedType.TypeData.Type.IsArray) {
396 object list = ReadListElement (info.MappedType, info.IsNullable, null, true);
397 if (list != null || info.IsNullable)
398 SetMemberValue (info.Member, ob, list, isValueList);
400 // If the member already has a list, reuse that list. No need to create a new one.
401 object list = GetMemberValue (info.Member, ob, isValueList);
403 list = CreateList (info.MappedType.TypeData.Type);
404 SetMemberValue (info.Member, ob, list, isValueList);
406 ReadListElement (info.MappedType, info.IsNullable, list, true);
409 readFlag[info.Member.Index] = true;
411 else if (info.Member.GetType() == typeof (XmlTypeMapMemberFlatList))
413 XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member;
414 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement (info), !IsReadOnly (info.Member, info.TypeData, ob, isValueList));
415 if (mem.ChoiceMember != null) {
416 AddListValue (mem.ChoiceTypeData, ref flatListsChoices [mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]-1, info.ChoiceValue, true);
419 else if (info.Member.GetType() == typeof (XmlTypeMapMemberAnyElement))
421 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member;
422 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
423 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
425 else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement))
428 readFlag[info.Member.Index] = true;
429 if (_format == SerializationFormat.Encoded)
431 if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive)
432 val = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
434 val = ReadReferencingElement (info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]);
436 if (info.MultiReferenceType) {
437 if (fixup.Ids[info.Member.Index] == null) // already read
438 SetMemberValue (info.Member, ob, val, isValueList);
440 else if (val != null)
441 SetMemberValue (info.Member, ob, val, isValueList);
444 SetMemberValue (info.Member, ob, ReadObjectElement (info), isValueList);
445 if (info.ChoiceValue != null) {
446 XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member;
447 imem.SetChoice (ob, info.ChoiceValue);
452 throw new InvalidOperationException ("Unknown member type");
454 else if (map.DefaultAnyElementMember != null)
456 XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember;
457 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
458 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
461 ProcessUnknownElement(ob);
463 else if (Reader.NodeType == System.Xml.XmlNodeType.Text && map.XmlTextCollector != null)
465 if (map.XmlTextCollector is XmlTypeMapMemberExpandable)
467 XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector;
468 XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList;
469 TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData;
471 object val = (itype.Type == typeof (string)) ? (object) Reader.ReadString() : (object) ReadXmlNode (itype, false);
472 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true);
476 XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement) map.XmlTextCollector;
477 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0];
478 if (info.TypeData.Type == typeof (string))
479 SetMemberValue (mem, ob, ReadString ((string) GetMemberValue (mem, ob, isValueList)), isValueList);
481 SetMemberValue (mem, ob, GetValueFromXmlString (Reader.ReadString(), info.TypeData, info.MappedType), isValueList);
487 Reader.MoveToContent();
490 if (flatLists != null)
492 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
494 Object list = flatLists[mem.FlatArrayIndex];
495 if (mem.TypeData.Type.IsArray)
496 list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true);
497 if (!IsReadOnly (mem, mem.TypeData, ob, isValueList) && mem.TypeData.Type.IsArray)
498 SetMemberValue (mem, ob, list, isValueList);
502 if (flatListsChoices != null)
504 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
506 Object list = flatListsChoices[mem.FlatArrayIndex];
507 if (list == null) continue;
508 list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.ChoiceTypeData.Type.GetElementType(), true);
509 XmlTypeMapMember.SetValue (ob, mem.ChoiceMember, list);
512 SetListMembersDefaults (map, ob, isValueList);
515 void SetListMembersDefaults (ClassMap map, object ob, bool isValueList)
517 if (map.ListMembers != null)
519 ArrayList members = map.ListMembers;
520 for (int n=0; n<members.Count; n++) {
521 XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
522 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
524 if (GetMemberValue (mem, ob, isValueList) == null)
525 SetMemberValue (mem, ob, InitializeList (mem.TypeData), isValueList);
530 internal void FixupMembers (ClassMap map, object obfixup, bool isValueList)
532 Fixup fixup = (Fixup)obfixup;
533 ICollection members = map.ElementMembers;
534 string[] ids = fixup.Ids;
535 foreach (XmlTypeMapMember member in members)
537 if (ids[member.Index] != null)
538 SetMemberValue (member, fixup.Source, GetTarget(ids[member.Index]), isValueList);
542 protected virtual void ProcessUnknownAttribute (object target)
544 UnknownNode (target);
547 protected virtual void ProcessUnknownElement (object target)
549 UnknownNode (target);
552 bool IsReadOnly (XmlTypeMapMember member, TypeData memType, object ob, bool isValueList)
554 if (isValueList) return !memType.HasPublicConstructor;
555 else return member.IsReadOnly (ob.GetType()) || !memType.HasPublicConstructor;
558 void SetMemberValue (XmlTypeMapMember member, object ob, object value, bool isValueList)
560 if (isValueList) ((object[])ob)[member.Index] = value;
562 member.SetValue (ob, value);
563 if (member.IsOptionalValueType)
564 member.SetValueSpecified (ob, true);
568 void SetMemberValueFromAttr (XmlTypeMapMember member, object ob, object value, bool isValueList)
570 // Enumeration values specified in custom attributes are stored as integer
571 // values if the custom attribute property is of type object. So, it is
572 // necessary to convert to the enum type before asigning the value to the field.
574 if (member.TypeData.Type.IsEnum)
575 value = Enum.ToObject (member.TypeData.Type, value);
576 SetMemberValue (member, ob, value, isValueList);
579 object GetMemberValue (XmlTypeMapMember member, object ob, bool isValueList)
581 if (isValueList) return ((object[])ob)[member.Index];
582 else return member.GetValue (ob);
585 object ReadObjectElement (XmlTypeMapElementInfo elem)
587 switch (elem.TypeData.SchemaType)
589 case SchemaTypes.XmlNode:
590 return ReadXmlNode (elem.TypeData, true);
592 case SchemaTypes.Primitive:
593 case SchemaTypes.Enum:
594 return ReadPrimitiveValue (elem);
596 case SchemaTypes.Array:
597 return ReadListElement (elem.MappedType, elem.IsNullable, null, true);
599 case SchemaTypes.Class:
600 return ReadObject (elem.MappedType, elem.IsNullable, true);
602 case SchemaTypes.XmlSerializable:
603 object ob = Activator.CreateInstance (elem.TypeData.Type);
604 return ReadSerializable ((IXmlSerializable)ob);
607 throw new NotSupportedException ("Invalid value type");
611 object ReadPrimitiveValue (XmlTypeMapElementInfo elem)
613 if (elem.TypeData.Type == typeof (XmlQualifiedName)) {
614 if (elem.IsNullable) return ReadNullableQualifiedName ();
615 else return ReadElementQualifiedName ();
617 else if (elem.IsNullable)
618 return GetValueFromXmlString (ReadNullableString (), elem.TypeData, elem.MappedType);
620 return GetValueFromXmlString (Reader.ReadElementString (), elem.TypeData, elem.MappedType);
623 object GetValueFromXmlString (string value, TypeData typeData, XmlTypeMapping typeMap)
625 if (typeData.SchemaType == SchemaTypes.Array)
626 return ReadListString (typeMap, value);
627 else if (typeData.SchemaType == SchemaTypes.Enum)
628 return GetEnumValue (typeMap, value);
629 else if (typeData.Type == typeof (XmlQualifiedName))
630 return ToXmlQualifiedName (value);
632 return XmlCustomFormatter.FromXmlString (typeData, value);
635 object ReadListElement (XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
637 Type listType = typeMap.TypeData.Type;
638 ListMap listMap = (ListMap)typeMap.ObjectMap;
640 if (listType.IsArray && ReadNull()) return null;
643 if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) list = CreateList (listType);
644 else throw CreateReadOnlyCollectionException (typeMap.TypeFullName);
647 if (Reader.IsEmptyElement) {
649 if (listType.IsArray)
650 list = ShrinkArray ((Array)list, 0, listType.GetElementType(), false);
655 Reader.ReadStartElement();
656 Reader.MoveToContent();
658 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
660 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
662 XmlTypeMapElementInfo elemInfo = listMap.FindElement (Reader.LocalName, Reader.NamespaceURI);
663 if (elemInfo != null)
664 AddListValue (typeMap.TypeData, ref list, index++, ReadObjectElement (elemInfo), false);
671 Reader.MoveToContent();
675 if (listType.IsArray)
676 list = ShrinkArray ((Array)list, index, listType.GetElementType(), false);
681 object ReadListString (XmlTypeMapping typeMap, string values)
683 Type listType = typeMap.TypeData.Type;
684 ListMap listMap = (ListMap)typeMap.ObjectMap;
685 values = values.Trim ();
687 if (values == string.Empty)
689 return Array.CreateInstance (listType.GetElementType(), 0);
692 string[] valueArray = values.Split (' ');
693 Array list = Array.CreateInstance (listType.GetElementType(), valueArray.Length);
695 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)listMap.ItemInfo[0];
697 for (int index = 0; index < valueArray.Length; index++)
698 list.SetValue (GetValueFromXmlString (valueArray[index], info.TypeData, info.MappedType), index);
703 void AddListValue (TypeData listType, ref object list, int index, object value, bool canCreateInstance)
705 Type type = listType.Type;
708 list = EnsureArrayIndex ((Array)list, index, type.GetElementType());
709 ((Array)list).SetValue (value, index);
711 else // Must be IEnumerable
714 if (canCreateInstance) list = Activator.CreateInstance (type);
715 else throw CreateReadOnlyCollectionException (type.FullName);
718 MethodInfo mi = type.GetMethod ("Add", new Type[] {listType.ListItemType} );
719 mi.Invoke (list, new object[] { value });
723 object CreateList (Type listType)
725 if (listType.IsArray)
726 return EnsureArrayIndex (null, 0, listType.GetElementType());
728 return Activator.CreateInstance (listType);
731 object InitializeList (TypeData listType)
733 if (listType.Type.IsArray)
736 return Activator.CreateInstance (listType.Type);
739 void FillList (object list, object items)
741 CopyEnumerableList (items, list);
744 void CopyEnumerableList (object source, object dest)
746 if (dest == null) throw CreateReadOnlyCollectionException (source.GetType().FullName);
748 object[] param = new object[1];
749 MethodInfo mi = dest.GetType().GetMethod ("Add");
750 foreach (object ob in (IEnumerable)source)
753 mi.Invoke (dest, param);
757 object ReadXmlNodeElement (XmlTypeMapping typeMap, bool isNullable)
759 return ReadXmlNode (typeMap.TypeData, false);
762 object ReadXmlNode (TypeData type, bool wrapped)
764 if (type.Type == typeof (XmlDocument))
765 return ReadXmlDocument (wrapped);
767 return ReadXmlNode (wrapped);
770 object ReadPrimitiveElement (XmlTypeMapping typeMap, bool isNullable)
772 XmlQualifiedName t = GetXsiType();
773 if (t == null) t = new XmlQualifiedName (typeMap.XmlType, typeMap.Namespace);
774 return ReadTypedPrimitive (t);
777 object ReadEnumElement (XmlTypeMapping typeMap, bool isNullable)
779 Reader.ReadStartElement ();
780 object o = GetEnumValue (typeMap, Reader.ReadString());
785 object GetEnumValue (XmlTypeMapping typeMap, string val)
787 EnumMap map = (EnumMap) typeMap.ObjectMap;
788 string ev = map.GetEnumName (val);
789 if (ev == null) throw CreateUnknownConstantException (val, typeMap.TypeData.Type);
790 return Enum.Parse (typeMap.TypeData.Type, ev);
793 object ReadXmlSerializableElement (XmlTypeMapping typeMap, bool isNullable)
795 Reader.MoveToContent ();
796 if (Reader.NodeType == XmlNodeType.Element)
798 if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace)
800 object ob = Activator.CreateInstance (typeMap.TypeData.Type);
801 return ReadSerializable ((IXmlSerializable)ob);
804 throw CreateUnknownNodeException ();
813 class FixupCallbackInfo
815 XmlSerializationReaderInterpreter _sri;
819 public FixupCallbackInfo (XmlSerializationReaderInterpreter sri, ClassMap map, bool isValueList)
823 _isValueList = isValueList;
826 public void FixupMembers (object fixup)
828 _sri.FixupMembers (_map, fixup, _isValueList);
832 class ReaderCallbackInfo
834 XmlSerializationReaderInterpreter _sri;
835 XmlTypeMapping _typeMap;
837 public ReaderCallbackInfo (XmlSerializationReaderInterpreter sri, XmlTypeMapping typeMap)
843 internal object ReadObject ()
845 return _sri.ReadObject (_typeMap, true, true);