XmlReflectionImporter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. //
  2. // System.Xml.Serialization.XmlReflectionImporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Erik LeBel ([email protected])
  7. // Lluis Sanchez Gual ([email protected])
  8. //
  9. // Copyright (C) Tim Coleman, 2002
  10. // (C) 2003 Erik LeBel
  11. //
  12. using System.Reflection;
  13. using System.Collections;
  14. namespace System.Xml.Serialization {
  15. public class XmlReflectionImporter {
  16. string initialDefaultNamespace;
  17. XmlAttributeOverrides attributeOverrides;
  18. ArrayList includedTypes;
  19. ReflectionHelper helper = new ReflectionHelper();
  20. int arrayChoiceCount = 1;
  21. #region Constructors
  22. public XmlReflectionImporter ()
  23. : this (null, null)
  24. {
  25. }
  26. public XmlReflectionImporter (string defaultNamespace)
  27. : this (null, defaultNamespace)
  28. {
  29. }
  30. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides)
  31. : this (attributeOverrides, null)
  32. {
  33. }
  34. public XmlReflectionImporter (XmlAttributeOverrides attributeOverrides, string defaultNamespace)
  35. {
  36. if (defaultNamespace == null)
  37. this.initialDefaultNamespace = String.Empty;
  38. else
  39. this.initialDefaultNamespace = defaultNamespace;
  40. if (attributeOverrides == null)
  41. this.attributeOverrides = new XmlAttributeOverrides();
  42. else
  43. this.attributeOverrides = attributeOverrides;
  44. }
  45. void Reset ()
  46. {
  47. helper = new ReflectionHelper();
  48. arrayChoiceCount = 1;
  49. }
  50. #endregion // Constructors
  51. #region Methods
  52. public XmlMembersMapping ImportMembersMapping (string elementName,
  53. string ns,
  54. XmlReflectionMember [] members,
  55. bool hasWrapperElement)
  56. {
  57. Reset ();
  58. XmlMemberMapping[] mapping = new XmlMemberMapping[members.Length];
  59. for (int n=0; n<members.Length; n++)
  60. {
  61. XmlTypeMapMember mapMem = CreateMapMember (members[n], ns);
  62. mapping[n] = new XmlMemberMapping (members[n], mapMem);
  63. }
  64. XmlMembersMapping mps = new XmlMembersMapping (elementName, ns, hasWrapperElement, mapping);
  65. mps.Format = SerializationFormat.Literal;
  66. return mps;
  67. }
  68. public XmlTypeMapping ImportTypeMapping (Type type)
  69. {
  70. return ImportTypeMapping (type, null, null);
  71. }
  72. public XmlTypeMapping ImportTypeMapping (Type type, string defaultNamespace)
  73. {
  74. return ImportTypeMapping (type, null, defaultNamespace);
  75. }
  76. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute group)
  77. {
  78. return ImportTypeMapping (type, group, null);
  79. }
  80. public XmlTypeMapping ImportTypeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  81. {
  82. if (type == null)
  83. throw new ArgumentNullException ("type");
  84. if (type == typeof (void))
  85. throw new InvalidOperationException ("Type " + type.Name + " may not be serialized.");
  86. if (defaultNamespace == null) defaultNamespace = initialDefaultNamespace;
  87. if (defaultNamespace == null) defaultNamespace = string.Empty;
  88. XmlTypeMapping map;
  89. switch (TypeTranslator.GetTypeData(type).SchemaType)
  90. {
  91. case SchemaTypes.Class: map = ImportClassMapping (type, root, defaultNamespace); break;
  92. case SchemaTypes.Array: map = ImportListMapping (type, root, defaultNamespace, null, 0); break;
  93. case SchemaTypes.XmlNode: map = ImportXmlNodeMapping (type, root, defaultNamespace); break;
  94. case SchemaTypes.Primitive: map = ImportPrimitiveMapping (type, root, defaultNamespace); break;
  95. case SchemaTypes.Enum: map = ImportEnumMapping (type, root, defaultNamespace); break;
  96. case SchemaTypes.XmlSerializable: map = ImportXmlSerializableMapping (type, root, defaultNamespace); break;
  97. default: throw new NotSupportedException ("Type " + type.FullName + " not supported for XML stialization");
  98. }
  99. map.Format = SerializationFormat.Literal;
  100. return map;
  101. }
  102. XmlTypeMapping CreateTypeMapping (TypeData typeData, XmlRootAttribute root, string defaultXmlType, string defaultNamespace)
  103. {
  104. string membersNamespace = defaultNamespace;
  105. string elementName;
  106. XmlAttributes atts = null;
  107. if (defaultXmlType == null) defaultXmlType = typeData.XmlType;
  108. if (!typeData.IsListType)
  109. {
  110. if (attributeOverrides != null)
  111. atts = attributeOverrides[typeData.Type];
  112. if (atts != null && typeData.SchemaType == SchemaTypes.Primitive)
  113. throw new InvalidOperationException ("XmlRoot and XmlType attributes may not be specified for the type " + typeData.FullTypeName);
  114. }
  115. if (atts == null)
  116. atts = new XmlAttributes (typeData.Type);
  117. if (atts.XmlRoot != null && root == null)
  118. root = atts.XmlRoot;
  119. if (atts.XmlType != null)
  120. {
  121. if (atts.XmlType.Namespace != null && atts.XmlType.Namespace != string.Empty)
  122. membersNamespace = atts.XmlType.Namespace;
  123. if (atts.XmlType.TypeName != null && atts.XmlType.TypeName != string.Empty)
  124. defaultXmlType = atts.XmlType.TypeName;
  125. }
  126. elementName = defaultXmlType;
  127. if (root != null)
  128. {
  129. if (root.ElementName != null && root.ElementName != String.Empty)
  130. elementName = root.ElementName;
  131. if (root.Namespace != null && root.Namespace != String.Empty)
  132. membersNamespace = root.Namespace;
  133. }
  134. if (membersNamespace == null) membersNamespace = "";
  135. XmlTypeMapping map = new XmlTypeMapping (elementName, membersNamespace, typeData, defaultXmlType);
  136. return map;
  137. }
  138. XmlTypeMapping ImportClassMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  139. {
  140. TypeData typeData = TypeTranslator.GetTypeData (type);
  141. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  142. if (map != null) return map;
  143. map = CreateTypeMapping (typeData, root, null, defaultNamespace);
  144. helper.RegisterClrType (map, type, defaultNamespace);
  145. helper.RegisterSchemaType (map, map.XmlType, defaultNamespace);
  146. ClassMap classMap = new ClassMap ();
  147. map.ObjectMap = classMap;
  148. // Import members
  149. // try
  150. // {
  151. ICollection members = GetReflectionMembers (type);
  152. foreach (XmlReflectionMember rmember in members)
  153. {
  154. if (rmember.XmlAttributes.XmlIgnore) continue;
  155. classMap.AddMember (CreateMapMember (rmember, map.Namespace));
  156. }
  157. // }
  158. // catch (Exception ex) {
  159. // throw helper.CreateError (map, ex.Message);
  160. // }
  161. // Import derived classes
  162. XmlIncludeAttribute[] includes = (XmlIncludeAttribute[])type.GetCustomAttributes (typeof (XmlIncludeAttribute), false);
  163. for (int n=0; n<includes.Length; n++)
  164. {
  165. Type includedType = includes[n].Type;
  166. if (!includedType.IsSubclassOf(type)) throw helper.CreateError (map, "Type '" + includedType.FullName + "' is not a subclass of '" + type.FullName + "'");
  167. XmlTypeMapping derived = ImportTypeMapping (includedType, root, defaultNamespace);
  168. map.DerivedTypes.Add (derived);
  169. map.DerivedTypes.AddRange (derived.DerivedTypes);
  170. }
  171. if (type == typeof (object) && includedTypes != null)
  172. {
  173. foreach (Type intype in includedTypes)
  174. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  175. }
  176. // Register this map as a derived class of object
  177. if (typeData.Type != typeof(object))
  178. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  179. return map;
  180. }
  181. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  182. {
  183. TypeData typeData = TypeTranslator.GetTypeData (type);
  184. ListMap obmap = new ListMap ();
  185. if (atts == null) atts = new XmlAttributes();
  186. Type itemType = typeData.ListItemType;
  187. // warning: byte[][] should not be considered multiarray
  188. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  189. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  190. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  191. {
  192. if (att.NestingLevel != nestingLevel) continue;
  193. Type elemType = (att.Type != null) ? att.Type : itemType;
  194. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType, att.DataType));
  195. elem.Namespace = att.Namespace != null ? att.Namespace : defaultNamespace;
  196. if (elem.Namespace == null) elem.Namespace = "";
  197. elem.Form = att.Form;
  198. elem.IsNullable = att.IsNullable;
  199. elem.NestingLevel = att.NestingLevel;
  200. if (isMultiArray)
  201. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  202. else if (elem.TypeData.IsComplexType)
  203. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  204. if (att.ElementName != null) elem.ElementName = att.ElementName;
  205. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  206. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  207. list.Add (elem);
  208. }
  209. if (list.Count == 0)
  210. {
  211. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  212. if (isMultiArray)
  213. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  214. else if (elem.TypeData.IsComplexType)
  215. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  216. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  217. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  218. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  219. elem.IsNullable = false;
  220. list.Add (elem);
  221. }
  222. obmap.ItemInfo = list;
  223. // If there can be different element names (types) in the array, then its name cannot
  224. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  225. string baseName;
  226. if (list.Count > 1)
  227. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  228. else
  229. {
  230. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  231. if (elem.MappedType != null) baseName = GetArrayName (elem.MappedType.ElementName);
  232. else baseName = GetArrayName (elem.ElementName);
  233. }
  234. // Avoid name colisions
  235. int nameCount = 1;
  236. string name = baseName;
  237. do {
  238. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  239. if (foundMap == null) nameCount = -1;
  240. else if (obmap.Equals (foundMap.ObjectMap) && typeData.Type == foundMap.TypeData.Type) return foundMap;
  241. else name = baseName + (nameCount++);
  242. }
  243. while (nameCount != -1);
  244. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  245. map.ObjectMap = obmap;
  246. // Register this map as a derived class of object
  247. helper.RegisterSchemaType (map, name, defaultNamespace);
  248. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  249. return map;
  250. }
  251. string GetArrayName (string elemName)
  252. {
  253. return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
  254. }
  255. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  256. {
  257. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  258. if (map != null) return map;
  259. // Registers the maps for XmlNode and XmlElement
  260. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  261. helper.RegisterClrType (nodeMap, typeof(XmlNode), defaultNamespace);
  262. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  263. helper.RegisterClrType (elemMap, typeof(XmlElement), defaultNamespace);
  264. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  265. helper.RegisterClrType (elemMap, typeof(XmlText), defaultNamespace);
  266. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  267. obmap.DerivedTypes.Add (nodeMap);
  268. obmap.DerivedTypes.Add (elemMap);
  269. obmap.DerivedTypes.Add (textMap);
  270. nodeMap.DerivedTypes.Add (elemMap);
  271. nodeMap.DerivedTypes.Add (textMap);
  272. return helper.GetRegisteredClrType (type, defaultNamespace);
  273. }
  274. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  275. {
  276. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  277. if (map != null) return map;
  278. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  279. helper.RegisterClrType (map, type, defaultNamespace);
  280. return map;
  281. }
  282. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  283. {
  284. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  285. if (map != null) return map;
  286. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  287. helper.RegisterClrType (map, type, defaultNamespace);
  288. string [] names = Enum.GetNames (type);
  289. ArrayList members = new ArrayList();
  290. foreach (string name in names)
  291. {
  292. MemberInfo[] mem = type.GetMember (name);
  293. string xmlName = name;
  294. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  295. if (atts.Length > 0) continue;
  296. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  297. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  298. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  299. }
  300. bool isFlags = type.GetCustomAttributes (typeof(FlagsAttribute),false).Length > 0;
  301. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)), isFlags);
  302. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  303. return map;
  304. }
  305. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  306. {
  307. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  308. if (map != null) return map;
  309. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  310. helper.RegisterClrType (map, type, defaultNamespace);
  311. return map;
  312. }
  313. public ICollection GetReflectionMembers (Type type)
  314. {
  315. ArrayList members = new ArrayList();
  316. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  317. foreach (PropertyInfo prop in properties)
  318. {
  319. if (!prop.CanRead) continue;
  320. XmlAttributes atts = attributeOverrides[type, prop.Name];
  321. if (atts == null) atts = new XmlAttributes (prop);
  322. if (atts.XmlIgnore) continue;
  323. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  324. members.Add (member);
  325. }
  326. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  327. foreach (FieldInfo field in fields)
  328. {
  329. XmlAttributes atts = attributeOverrides[type, field.Name];
  330. if (atts == null) atts = new XmlAttributes (field);
  331. if (atts.XmlIgnore) continue;
  332. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  333. members.Add (member);
  334. }
  335. return members;
  336. }
  337. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  338. {
  339. XmlTypeMapMember mapMember;
  340. XmlAttributes atts = rmember.XmlAttributes;
  341. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  342. if (atts.XmlAnyAttribute != null)
  343. {
  344. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  345. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  346. {
  347. mapMember = new XmlTypeMapMemberAnyAttribute();
  348. }
  349. else
  350. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  351. }
  352. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  353. {
  354. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  355. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  356. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  357. {
  358. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  359. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, member, atts);
  360. mapMember = member;
  361. }
  362. else
  363. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  364. }
  365. else if (atts.Xmlns)
  366. {
  367. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  368. mapMember = mapNamespaces;
  369. }
  370. else if (atts.XmlAttribute != null)
  371. {
  372. // An attribute
  373. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  374. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  375. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  376. if (atts.XmlAttribute.AttributeName == null)
  377. mapAttribute.AttributeName = rmember.MemberName;
  378. else
  379. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  380. mapAttribute.DataType = atts.XmlAttribute.DataType;
  381. mapAttribute.Form = atts.XmlAttribute.Form;
  382. mapAttribute.Namespace = (atts.XmlAttribute.Namespace != null) ? atts.XmlAttribute.Namespace : "";
  383. if (typeData.IsComplexType)
  384. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  385. mapMember = mapAttribute;
  386. }
  387. else if (typeData.SchemaType == SchemaTypes.Array)
  388. {
  389. // If the member has a single XmlElementAttribute and the type is the type of the member,
  390. // then it is not a flat list
  391. if (atts.XmlElements.Count > 1 ||
  392. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  393. (atts.XmlText != null))
  394. {
  395. // A flat list
  396. // TODO: check that it does not have XmlArrayAttribute
  397. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  398. member.ListMap = new ListMap ();
  399. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  400. member.ElementInfo = member.ListMap.ItemInfo;
  401. mapMember = member;
  402. }
  403. else
  404. {
  405. // A list
  406. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  407. // Creates an ElementInfo that identifies the array instance.
  408. member.ElementInfo = new XmlTypeMapElementInfoList();
  409. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  410. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  411. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  412. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  413. member.ElementInfo.Add (elem);
  414. mapMember = member;
  415. }
  416. }
  417. else
  418. {
  419. // An element
  420. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  421. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  422. mapMember = member;
  423. }
  424. mapMember.DefaultValue = atts.XmlDefaultValue;
  425. mapMember.TypeData = typeData;
  426. mapMember.Name = rmember.MemberName;
  427. return mapMember;
  428. }
  429. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  430. {
  431. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  432. if (atts.XmlText != null)
  433. {
  434. member.IsXmlTextCollector = true;
  435. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  436. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  437. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType, atts.XmlText.DataType));
  438. if (elem.TypeData.SchemaType != SchemaTypes.Primitive && elem.TypeData.SchemaType != SchemaTypes.Enum &&
  439. elem.TypeData.SchemaType != SchemaTypes.XmlNode)
  440. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  441. elem.ElementName = "<text>";
  442. elem.Namespace = string.Empty;
  443. elem.WrappedElement = false;
  444. list.Add (elem);
  445. }
  446. if (atts.XmlElements.Count == 0)
  447. {
  448. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  449. elem.ElementName = defaultName;
  450. elem.Namespace = defaultNamespace;
  451. if (elem.TypeData.IsComplexType)
  452. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  453. list.Add (elem);
  454. }
  455. bool multiType = (atts.XmlElements.Count > 1);
  456. foreach (XmlElementAttribute att in atts.XmlElements)
  457. {
  458. Type elemType = (att.Type != null) ? att.Type : defaultType;
  459. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType, att.DataType));
  460. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  461. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  462. elem.Form = att.Form;
  463. elem.IsNullable = att.IsNullable;
  464. if (elem.TypeData.IsComplexType)
  465. {
  466. if (att.DataType != null) throw new InvalidOperationException ("'" + att.DataType + "' is an invalid value for the XmlElementAttribute.DateTime property. The property may only be specified for primitive types.");
  467. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  468. }
  469. if (att.ElementName != null)
  470. elem.ElementName = att.ElementName;
  471. else if (multiType) {
  472. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  473. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  474. }
  475. else
  476. elem.ElementName = defaultName;
  477. list.Add (elem);
  478. }
  479. return list;
  480. }
  481. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlTypeMapMemberElement member, XmlAttributes atts)
  482. {
  483. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  484. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  485. {
  486. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  487. elem.ElementName = (att.Name != null) ? att.Name : "";
  488. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  489. list.Add (elem);
  490. }
  491. return list;
  492. }
  493. public void IncludeType (Type type)
  494. {
  495. if (type == null)
  496. throw new ArgumentNullException ("type");
  497. if (includedTypes == null) includedTypes = new ArrayList ();
  498. includedTypes.Add (type);
  499. }
  500. #endregion // Methods
  501. }
  502. }