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, true, 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;
310 if (map.ElementMembers != null) maxInd = map.ElementMembers.Count;
314 maxInd = int.MaxValue;
316 if (map.FlatLists != null)
318 indexes = new int[map.FlatLists.Count];
319 flatLists = new object[map.FlatLists.Count];
320 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
321 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
322 flatLists [mem.FlatArrayIndex] = mem.GetValue (ob);
324 flatLists [mem.FlatArrayIndex] = InitializeList (mem.TypeData);
327 if (_format == SerializationFormat.Encoded && map.ElementMembers != null)
329 FixupCallbackInfo info = new FixupCallbackInfo (this, map, isValueList);
330 fixup = new Fixup(ob, new XmlSerializationFixupCallback(info.FixupMembers), map.ElementMembers.Count);
334 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && (ind < maxInd))
336 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
338 XmlTypeMapElementInfo info;
341 info = map.GetElement (ind++);
343 else if (hasAnyReturnMember) {
344 info = (XmlTypeMapElementInfo) ((XmlTypeMapMemberElement)map.ReturnMember).ElementInfo[0];
345 hasAnyReturnMember = false;
348 info = map.GetElement (Reader.LocalName, Reader.NamespaceURI);
350 if (info != null && !readFlag[info.Member.Index] )
352 if (info.Member.GetType() == typeof (XmlTypeMapMemberList))
354 if (_format == SerializationFormat.Encoded && info.MultiReferenceType)
356 object list = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
357 if (fixup.Ids[info.Member.Index] == null) // Already read
359 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) throw CreateReadOnlyCollectionException (info.TypeData.FullTypeName);
360 else SetMemberValue (info.Member, ob, list, isValueList);
362 else if (!info.MappedType.TypeData.Type.IsArray)
364 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList))
365 list = GetMemberValue (info.Member, ob, isValueList);
367 list = CreateList (info.MappedType.TypeData.Type);
368 SetMemberValue (info.Member, ob, list, isValueList);
370 AddFixup (new CollectionFixup (list, new XmlSerializationCollectionFixupCallback (FillList), fixup.Ids[info.Member.Index]));
371 fixup.Ids[info.Member.Index] = null; // The member already has the value, no further fix needed.
376 if (IsReadOnly (info.Member, info.TypeData, ob, isValueList)) ReadListElement (info.MappedType, info.IsNullable, GetMemberValue (info.Member, ob, isValueList), false);
377 else SetMemberValue (info.Member, ob, ReadListElement (info.MappedType, info.IsNullable, null, true), isValueList);
379 readFlag[info.Member.Index] = true;
381 else if (info.Member.GetType() == typeof (XmlTypeMapMemberFlatList))
383 XmlTypeMapMemberFlatList mem = (XmlTypeMapMemberFlatList)info.Member;
384 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadObjectElement (info), !IsReadOnly (info.Member, info.TypeData, ob, isValueList));
386 else if (info.Member.GetType() == typeof (XmlTypeMapMemberAnyElement))
388 XmlTypeMapMemberAnyElement mem = (XmlTypeMapMemberAnyElement)info.Member;
389 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
390 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
392 else if (info.Member.GetType() == typeof(XmlTypeMapMemberElement))
395 readFlag[info.Member.Index] = true;
396 if (_format == SerializationFormat.Encoded)
398 if (info.Member.TypeData.SchemaType != SchemaTypes.Primitive)
399 val = ReadReferencingElement (out fixup.Ids[info.Member.Index]);
401 val = ReadReferencingElement (info.Member.TypeData.XmlType, System.Xml.Schema.XmlSchema.Namespace, out fixup.Ids[info.Member.Index]);
403 if (info.MultiReferenceType) {
404 if (fixup.Ids[info.Member.Index] == null) // already read
405 SetMemberValue (info.Member, ob, val, isValueList);
407 else if (val != null)
408 SetMemberValue (info.Member, ob, val, isValueList);
411 SetMemberValue (info.Member, ob, ReadObjectElement (info), isValueList);
412 if (info.ChoiceValue != null) {
413 XmlTypeMapMemberElement imem = (XmlTypeMapMemberElement) info.Member;
414 imem.SetChoice (ob, info.ChoiceValue);
419 throw new InvalidOperationException ("Unknown member type");
421 else if (map.DefaultAnyElementMember != null)
423 XmlTypeMapMemberAnyElement mem = map.DefaultAnyElementMember;
424 if (mem.TypeData.IsListType) AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, ReadXmlNode (mem.TypeData.ListItemTypeData, false), true);
425 else SetMemberValue (mem, ob, ReadXmlNode (mem.TypeData, false), isValueList);
428 ProcessUnknownElement(ob);
430 else if (Reader.NodeType == System.Xml.XmlNodeType.Text && map.XmlTextCollector != null)
432 if (map.XmlTextCollector is XmlTypeMapMemberExpandable)
434 XmlTypeMapMemberExpandable mem = (XmlTypeMapMemberExpandable)map.XmlTextCollector;
435 XmlTypeMapMemberFlatList flatl = mem as XmlTypeMapMemberFlatList;
436 TypeData itype = (flatl == null) ? mem.TypeData.ListItemTypeData : flatl.ListMap.FindTextElement().TypeData;
438 object val = (itype.Type == typeof (string)) ? (object) Reader.ReadString() : (object) ReadXmlNode (itype, false);
439 AddListValue (mem.TypeData, ref flatLists[mem.FlatArrayIndex], indexes[mem.FlatArrayIndex]++, val, true);
443 XmlTypeMapMemberElement mem = (XmlTypeMapMemberElement) map.XmlTextCollector;
444 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo) mem.ElementInfo [0];
445 if (info.TypeData.Type == typeof (string))
446 SetMemberValue (mem, ob, ReadString ((string) GetMemberValue (mem, ob, isValueList)), isValueList);
448 SetMemberValue (mem, ob, GetValueFromXmlString (Reader.ReadString(), info.TypeData, info.MappedType), isValueList);
454 Reader.MoveToContent();
457 if (flatLists != null)
459 foreach (XmlTypeMapMemberExpandable mem in map.FlatLists)
461 Object list = flatLists[mem.FlatArrayIndex];
462 if (mem.TypeData.Type.IsArray)
463 list = ShrinkArray ((Array)list, indexes[mem.FlatArrayIndex], mem.TypeData.Type.GetElementType(), true);
464 if (!IsReadOnly (mem, mem.TypeData, ob, isValueList))
465 SetMemberValue (mem, ob, list, isValueList);
468 SetListMembersDefaults (map, ob, isValueList);
471 void SetListMembersDefaults (ClassMap map, object ob, bool isValueList)
473 if (map.ListMembers != null)
475 ArrayList members = map.ListMembers;
476 for (int n=0; n<members.Count; n++) {
477 XmlTypeMapMember mem = (XmlTypeMapMember) members[n];
478 if (IsReadOnly (mem, mem.TypeData, ob, isValueList))
480 if (GetMemberValue (mem, ob, isValueList) == null)
481 SetMemberValue (mem, ob, InitializeList (mem.TypeData), isValueList);
486 internal void FixupMembers (ClassMap map, object obfixup, bool isValueList)
488 Fixup fixup = (Fixup)obfixup;
489 ICollection members = map.ElementMembers;
490 string[] ids = fixup.Ids;
491 foreach (XmlTypeMapMember member in members)
493 if (ids[member.Index] != null)
494 SetMemberValue (member, fixup.Source, GetTarget(ids[member.Index]), isValueList);
498 protected virtual void ProcessUnknownAttribute (object target)
500 UnknownNode (target);
503 protected virtual void ProcessUnknownElement (object target)
505 UnknownNode (target);
508 bool IsReadOnly (XmlTypeMapMember member, TypeData memType, object ob, bool isValueList)
510 if (isValueList) return !memType.HasPublicConstructor;
511 else return member.IsReadOnly (ob.GetType()) || !memType.HasPublicConstructor;
514 void SetMemberValue (XmlTypeMapMember member, object ob, object value, bool isValueList)
516 if (isValueList) ((object[])ob)[member.Index] = value;
518 member.SetValue (ob, value);
519 if (member.IsOptionalValueType)
520 member.SetValueSpecified (ob, true);
524 void SetMemberValueFromAttr (XmlTypeMapMember member, object ob, object value, bool isValueList)
526 // Enumeration values specified in custom attributes are stored as integer
527 // values if the custom attribute property is of type object. So, it is
528 // necessary to convert to the enum type before asigning the value to the field.
530 if (member.TypeData.Type.IsEnum)
531 value = Enum.ToObject (member.TypeData.Type, value);
532 SetMemberValue (member, ob, value, isValueList);
535 object GetMemberValue (XmlTypeMapMember member, object ob, bool isValueList)
537 if (isValueList) return ((object[])ob)[member.Index];
538 else return member.GetValue (ob);
541 object ReadObjectElement (XmlTypeMapElementInfo elem)
543 switch (elem.TypeData.SchemaType)
545 case SchemaTypes.XmlNode:
546 return ReadXmlNode (elem.TypeData, true);
548 case SchemaTypes.Primitive:
549 case SchemaTypes.Enum:
550 return ReadPrimitiveValue (elem);
552 case SchemaTypes.Array:
553 return ReadListElement (elem.MappedType, elem.IsNullable, null, true);
555 case SchemaTypes.Class:
556 return ReadObject (elem.MappedType, elem.IsNullable, true);
558 case SchemaTypes.XmlSerializable:
559 object ob = Activator.CreateInstance (elem.TypeData.Type);
560 return ReadSerializable ((IXmlSerializable)ob);
563 throw new NotSupportedException ("Invalid value type");
567 object ReadPrimitiveValue (XmlTypeMapElementInfo elem)
569 if (elem.TypeData.Type == typeof (XmlQualifiedName)) {
570 if (elem.IsNullable) return ReadNullableQualifiedName ();
571 else return ReadElementQualifiedName ();
573 else if (elem.IsNullable)
574 return GetValueFromXmlString (ReadNullableString (), elem.TypeData, elem.MappedType);
576 return GetValueFromXmlString (Reader.ReadElementString (), elem.TypeData, elem.MappedType);
579 object GetValueFromXmlString (string value, TypeData typeData, XmlTypeMapping typeMap)
581 if (typeData.SchemaType == SchemaTypes.Array)
582 return ReadListString (typeMap, value);
583 else if (typeData.SchemaType == SchemaTypes.Enum)
584 return GetEnumValue (typeMap, value);
585 else if (typeData.Type == typeof (XmlQualifiedName))
586 return ToXmlQualifiedName (value);
588 return XmlCustomFormatter.FromXmlString (typeData, value);
591 object ReadListElement (XmlTypeMapping typeMap, bool isNullable, object list, bool canCreateInstance)
593 Type listType = typeMap.TypeData.Type;
594 ListMap listMap = (ListMap)typeMap.ObjectMap;
596 if (ReadNull()) return null;
599 if (canCreateInstance && typeMap.TypeData.HasPublicConstructor) list = CreateList (listType);
600 else throw CreateReadOnlyCollectionException (typeMap.TypeFullName);
603 if (Reader.IsEmptyElement) {
605 if (listType.IsArray)
606 list = ShrinkArray ((Array)list, 0, listType.GetElementType(), isNullable);
611 Reader.ReadStartElement();
612 Reader.MoveToContent();
614 while (Reader.NodeType != System.Xml.XmlNodeType.EndElement)
616 if (Reader.NodeType == System.Xml.XmlNodeType.Element)
618 XmlTypeMapElementInfo elemInfo = listMap.FindElement (Reader.LocalName, Reader.NamespaceURI);
619 if (elemInfo != null)
620 AddListValue (typeMap.TypeData, ref list, index++, ReadObjectElement (elemInfo), false);
627 Reader.MoveToContent();
631 if (listType.IsArray)
632 list = ShrinkArray ((Array)list, index, listType.GetElementType(), isNullable);
637 object ReadListString (XmlTypeMapping typeMap, string values)
639 Type listType = typeMap.TypeData.Type;
640 ListMap listMap = (ListMap)typeMap.ObjectMap;
641 values = values.Trim ();
643 if (values == string.Empty)
645 return Array.CreateInstance (listType.GetElementType(), 0);
648 string[] valueArray = values.Split (' ');
649 Array list = Array.CreateInstance (listType.GetElementType(), valueArray.Length);
651 XmlTypeMapElementInfo info = (XmlTypeMapElementInfo)listMap.ItemInfo[0];
653 for (int index = 0; index < valueArray.Length; index++)
654 list.SetValue (GetValueFromXmlString (valueArray[index], info.TypeData, info.MappedType), index);
659 void AddListValue (TypeData listType, ref object list, int index, object value, bool canCreateInstance)
661 Type type = listType.Type;
664 list = EnsureArrayIndex ((Array)list, index, type.GetElementType());
665 ((Array)list).SetValue (value, index);
667 else // Must be IEnumerable
670 if (canCreateInstance) list = Activator.CreateInstance (type);
671 else throw CreateReadOnlyCollectionException (type.FullName);
674 MethodInfo mi = type.GetMethod ("Add", new Type[] {listType.ListItemType} );
675 mi.Invoke (list, new object[] { value });
679 object CreateList (Type listType)
681 if (listType.IsArray)
682 return EnsureArrayIndex (null, 0, listType.GetElementType());
684 return Activator.CreateInstance (listType);
687 object InitializeList (TypeData listType)
689 if (listType.Type.IsArray)
692 return Activator.CreateInstance (listType.Type);
695 void FillList (object list, object items)
697 CopyEnumerableList (items, list);
700 void CopyEnumerableList (object source, object dest)
702 if (dest == null) throw CreateReadOnlyCollectionException (source.GetType().FullName);
704 object[] param = new object[1];
705 MethodInfo mi = dest.GetType().GetMethod ("Add");
706 foreach (object ob in (IEnumerable)source)
709 mi.Invoke (dest, param);
713 object ReadXmlNodeElement (XmlTypeMapping typeMap, bool isNullable)
715 return ReadXmlNode (typeMap.TypeData, false);
718 object ReadXmlNode (TypeData type, bool wrapped)
720 if (type.Type == typeof (XmlDocument))
721 return ReadXmlDocument (wrapped);
723 return ReadXmlNode (wrapped);
726 object ReadPrimitiveElement (XmlTypeMapping typeMap, bool isNullable)
728 XmlQualifiedName t = GetXsiType();
729 if (t == null) t = new XmlQualifiedName (typeMap.XmlType, typeMap.Namespace);
730 return ReadTypedPrimitive (t);
733 object ReadEnumElement (XmlTypeMapping typeMap, bool isNullable)
735 Reader.ReadStartElement ();
736 object o = GetEnumValue (typeMap, Reader.ReadString());
737 Reader.ReadEndElement ();
741 object GetEnumValue (XmlTypeMapping typeMap, string val)
743 EnumMap map = (EnumMap) typeMap.ObjectMap;
744 string ev = map.GetEnumName (val);
745 if (ev == null) throw CreateUnknownConstantException (val, typeMap.TypeData.Type);
746 return Enum.Parse (typeMap.TypeData.Type, ev);
749 object ReadXmlSerializableElement (XmlTypeMapping typeMap, bool isNullable)
751 Reader.MoveToContent ();
752 if (Reader.NodeType == XmlNodeType.Element)
754 if (Reader.LocalName == typeMap.ElementName && Reader.NamespaceURI == typeMap.Namespace)
756 object ob = Activator.CreateInstance (typeMap.TypeData.Type);
757 return ReadSerializable ((IXmlSerializable)ob);
760 throw CreateUnknownNodeException ();
769 class FixupCallbackInfo
771 XmlSerializationReaderInterpreter _sri;
775 public FixupCallbackInfo (XmlSerializationReaderInterpreter sri, ClassMap map, bool isValueList)
779 _isValueList = isValueList;
782 public void FixupMembers (object fixup)
784 _sri.FixupMembers (_map, fixup, _isValueList);
788 class ReaderCallbackInfo
790 XmlSerializationReaderInterpreter _sri;
791 XmlTypeMapping _typeMap;
793 public ReaderCallbackInfo (XmlSerializationReaderInterpreter sri, XmlTypeMapping typeMap)
799 internal object ReadObject ()
801 return _sri.ReadObject (_typeMap, true, true);