XmlReflectionImporter.cs 21 KB

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