XmlReflectionImporter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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. map.DerivedTypes.Add (ImportTypeMapping (includedType, root, defaultNamespace));
  168. }
  169. if (type == typeof (object) && includedTypes != null)
  170. {
  171. foreach (Type intype in includedTypes)
  172. map.DerivedTypes.Add (ImportTypeMapping (intype, defaultNamespace));
  173. }
  174. // Register this map as a derived class of object
  175. if (typeData.Type != typeof(object))
  176. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  177. return map;
  178. }
  179. XmlTypeMapping ImportListMapping (Type type, XmlRootAttribute root, string defaultNamespace, XmlAttributes atts, int nestingLevel)
  180. {
  181. TypeData typeData = TypeTranslator.GetTypeData (type);
  182. ListMap obmap = new ListMap ();
  183. if (atts == null) atts = new XmlAttributes();
  184. Type itemType = typeData.ListItemType;
  185. // warning: byte[][] should not be considered multiarray
  186. bool isMultiArray = (type.IsArray && (TypeTranslator.GetTypeData(itemType).SchemaType == SchemaTypes.Array) && itemType.IsArray);
  187. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  188. foreach (XmlArrayItemAttribute att in atts.XmlArrayItems)
  189. {
  190. if (att.NestingLevel != nestingLevel) continue;
  191. Type elemType = (att.Type != null) ? att.Type : itemType;
  192. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData(elemType));
  193. elem.DataType = att.DataType;
  194. elem.Namespace = att.Namespace != null ? att.Namespace : "";
  195. elem.Form = att.Form;
  196. elem.IsNullable = att.IsNullable;
  197. elem.NestingLevel = att.NestingLevel;
  198. if (isMultiArray)
  199. elem.MappedType = ImportListMapping (elemType, null, elem.Namespace, atts, nestingLevel + 1);
  200. else if (elem.TypeData.IsComplexType)
  201. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  202. if (att.ElementName != null) elem.ElementName = att.ElementName;
  203. else if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  204. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  205. list.Add (elem);
  206. }
  207. if (list.Count == 0)
  208. {
  209. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (null, TypeTranslator.GetTypeData (itemType));
  210. if (isMultiArray)
  211. elem.MappedType = ImportListMapping (itemType, null, defaultNamespace, atts, nestingLevel + 1);
  212. else if (elem.TypeData.IsComplexType)
  213. elem.MappedType = ImportTypeMapping (itemType, null, defaultNamespace);
  214. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  215. else elem.ElementName = TypeTranslator.GetTypeData(itemType).XmlType ;
  216. elem.Namespace = (defaultNamespace != null) ? defaultNamespace : "";
  217. elem.IsNullable = true; // By default, items are nullable
  218. list.Add (elem);
  219. }
  220. obmap.ItemInfo = list;
  221. // If there can be different element names (types) in the array, then its name cannot
  222. // be "ArrayOfXXX" it must be something like ArrayOfChoiceNNN
  223. string baseName;
  224. if (list.Count > 1)
  225. baseName = "ArrayOfChoice" + (arrayChoiceCount++);
  226. else
  227. {
  228. XmlTypeMapElementInfo elem = ((XmlTypeMapElementInfo)list[0]);
  229. if (elem.MappedType != null) baseName = GetArrayName (elem.MappedType.ElementName);
  230. else baseName = GetArrayName (elem.ElementName);
  231. }
  232. // Avoid name colisions
  233. int nameCount = 1;
  234. string name = baseName;
  235. do {
  236. XmlTypeMapping foundMap = helper.GetRegisteredSchemaType (name, defaultNamespace);
  237. if (foundMap == null) nameCount = -1;
  238. else if (obmap.Equals (foundMap.ObjectMap)) return foundMap;
  239. else name = baseName + (nameCount++);
  240. }
  241. while (nameCount != -1);
  242. XmlTypeMapping map = CreateTypeMapping (typeData, root, name, defaultNamespace);
  243. map.ObjectMap = obmap;
  244. // Register this map as a derived class of object
  245. helper.RegisterSchemaType (map, name, defaultNamespace);
  246. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  247. return map;
  248. }
  249. string GetArrayName (string elemName)
  250. {
  251. return "ArrayOf" + Char.ToUpper (elemName [0]) + elemName.Substring (1);
  252. }
  253. XmlTypeMapping ImportXmlNodeMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  254. {
  255. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  256. if (map != null) return map;
  257. // Registers the maps for XmlNode and XmlElement
  258. XmlTypeMapping nodeMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlNode)), root, null, defaultNamespace);
  259. helper.RegisterClrType (nodeMap, typeof(XmlNode), defaultNamespace);
  260. XmlTypeMapping elemMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlElement)), root, null, defaultNamespace);
  261. helper.RegisterClrType (elemMap, typeof(XmlElement), defaultNamespace);
  262. XmlTypeMapping textMap = CreateTypeMapping (TypeTranslator.GetTypeData (typeof(XmlText)), root, null, defaultNamespace);
  263. helper.RegisterClrType (elemMap, typeof(XmlText), defaultNamespace);
  264. XmlTypeMapping obmap = ImportTypeMapping (typeof(object));
  265. obmap.DerivedTypes.Add (nodeMap);
  266. obmap.DerivedTypes.Add (elemMap);
  267. obmap.DerivedTypes.Add (textMap);
  268. nodeMap.DerivedTypes.Add (elemMap);
  269. nodeMap.DerivedTypes.Add (textMap);
  270. return helper.GetRegisteredClrType (type, defaultNamespace);
  271. }
  272. XmlTypeMapping ImportPrimitiveMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  273. {
  274. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  275. if (map != null) return map;
  276. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  277. helper.RegisterClrType (map, type, defaultNamespace);
  278. return map;
  279. }
  280. XmlTypeMapping ImportEnumMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  281. {
  282. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  283. if (map != null) return map;
  284. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  285. helper.RegisterClrType (map, type, defaultNamespace);
  286. string [] names = Enum.GetNames (type);
  287. ArrayList members = new ArrayList();
  288. foreach (string name in names)
  289. {
  290. MemberInfo[] mem = type.GetMember (name);
  291. string xmlName = name;
  292. object[] atts = mem[0].GetCustomAttributes (typeof(XmlIgnoreAttribute), false);
  293. if (atts.Length > 0) continue;
  294. atts = mem[0].GetCustomAttributes (typeof(XmlEnumAttribute), false);
  295. if (atts.Length > 0) xmlName = ((XmlEnumAttribute)atts[0]).Name;
  296. members.Add (new EnumMap.EnumMapMember (xmlName, name));
  297. }
  298. map.ObjectMap = new EnumMap ((EnumMap.EnumMapMember[])members.ToArray (typeof(EnumMap.EnumMapMember)));
  299. ImportTypeMapping (typeof(object)).DerivedTypes.Add (map);
  300. return map;
  301. }
  302. XmlTypeMapping ImportXmlSerializableMapping (Type type, XmlRootAttribute root, string defaultNamespace)
  303. {
  304. XmlTypeMapping map = helper.GetRegisteredClrType (type, defaultNamespace);
  305. if (map != null) return map;
  306. map = CreateTypeMapping (TypeTranslator.GetTypeData (type), root, null, defaultNamespace);
  307. helper.RegisterClrType (map, type, defaultNamespace);
  308. return map;
  309. }
  310. public ICollection GetReflectionMembers (Type type)
  311. {
  312. ArrayList members = new ArrayList();
  313. PropertyInfo[] properties = type.GetProperties (BindingFlags.Instance | BindingFlags.Public);
  314. foreach (PropertyInfo prop in properties)
  315. {
  316. if (!prop.CanRead) continue;
  317. XmlAttributes atts = attributeOverrides[type, prop.Name];
  318. if (atts == null) atts = new XmlAttributes (prop);
  319. if (atts.XmlIgnore) continue;
  320. XmlReflectionMember member = new XmlReflectionMember(prop.Name, prop.PropertyType, atts);
  321. members.Add (member);
  322. }
  323. FieldInfo[] fields = type.GetFields (BindingFlags.Instance | BindingFlags.Public);
  324. foreach (FieldInfo field in fields)
  325. {
  326. XmlAttributes atts = attributeOverrides[type, field.Name];
  327. if (atts == null) atts = new XmlAttributes (field);
  328. if (atts.XmlIgnore) continue;
  329. XmlReflectionMember member = new XmlReflectionMember(field.Name, field.FieldType, atts);
  330. members.Add (member);
  331. }
  332. return members;
  333. }
  334. private XmlTypeMapMember CreateMapMember (XmlReflectionMember rmember, string defaultNamespace)
  335. {
  336. XmlTypeMapMember mapMember;
  337. XmlAttributes atts = rmember.XmlAttributes;
  338. TypeData typeData = TypeTranslator.GetTypeData (rmember.MemberType);
  339. if (atts.XmlAnyAttribute != null)
  340. {
  341. if ( (rmember.MemberType.FullName == "System.Xml.XmlAttribute[]") ||
  342. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") )
  343. {
  344. mapMember = new XmlTypeMapMemberAnyAttribute();
  345. }
  346. else
  347. throw new InvalidOperationException ("XmlAnyAttributeAttribute can only be applied to members of type XmlAttribute[] or XmlNode[]");
  348. }
  349. else if (atts.XmlAnyElements != null && atts.XmlAnyElements.Count > 0)
  350. {
  351. if ( (rmember.MemberType.FullName == "System.Xml.XmlElement[]") ||
  352. (rmember.MemberType.FullName == "System.Xml.XmlNode[]") ||
  353. (rmember.MemberType.FullName == "System.Xml.XmlElement"))
  354. {
  355. XmlTypeMapMemberAnyElement member = new XmlTypeMapMemberAnyElement();
  356. member.ElementInfo = ImportAnyElementInfo (defaultNamespace, member, atts);
  357. mapMember = member;
  358. }
  359. else
  360. throw new InvalidOperationException ("XmlAnyElementAttribute can only be applied to members of type XmlElement, XmlElement[] or XmlNode[]");
  361. }
  362. else if (atts.Xmlns)
  363. {
  364. XmlTypeMapMemberNamespaces mapNamespaces = new XmlTypeMapMemberNamespaces ();
  365. mapMember = mapNamespaces;
  366. }
  367. else if (atts.XmlAttribute != null)
  368. {
  369. // An attribute
  370. if (atts.XmlElements != null && atts.XmlElements.Count > 0)
  371. throw new Exception ("XmlAttributeAttribute and XmlElementAttribute cannot be applied to the same member");
  372. XmlTypeMapMemberAttribute mapAttribute = new XmlTypeMapMemberAttribute ();
  373. if (atts.XmlAttribute.AttributeName == null)
  374. mapAttribute.AttributeName = rmember.MemberName;
  375. else
  376. mapAttribute.AttributeName = atts.XmlAttribute.AttributeName;
  377. mapAttribute.DataType = atts.XmlAttribute.DataType;
  378. mapAttribute.Form = atts.XmlAttribute.Form;
  379. mapAttribute.Namespace = (atts.XmlAttribute.Namespace != null) ? atts.XmlAttribute.Namespace : "";
  380. if (typeData.IsComplexType)
  381. mapAttribute.MappedType = ImportTypeMapping (typeData.Type, null, mapAttribute.Namespace);
  382. mapMember = mapAttribute;
  383. }
  384. else if (typeData.SchemaType == SchemaTypes.Array)
  385. {
  386. // If the member has a single XmlElementAttribute and the type is the type of the member,
  387. // then it is not a flat list
  388. if (atts.XmlElements.Count > 1 ||
  389. (atts.XmlElements.Count == 1 && atts.XmlElements[0].Type != typeData.Type) ||
  390. (atts.XmlText != null))
  391. {
  392. // A flat list
  393. // TODO: check that it does not have XmlArrayAttribute
  394. XmlTypeMapMemberFlatList member = new XmlTypeMapMemberFlatList ();
  395. member.ListMap = new ListMap ();
  396. member.ListMap.ItemInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, typeData.ListItemType, member, atts);
  397. member.ElementInfo = member.ListMap.ItemInfo;
  398. mapMember = member;
  399. }
  400. else
  401. {
  402. // A list
  403. XmlTypeMapMemberList member = new XmlTypeMapMemberList ();
  404. // Creates an ElementInfo that identifies the array instance.
  405. member.ElementInfo = new XmlTypeMapElementInfoList();
  406. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, typeData);
  407. elem.ElementName = (atts.XmlArray != null && atts.XmlArray.ElementName != null) ? atts.XmlArray.ElementName : rmember.MemberName;
  408. elem.Namespace = (atts.XmlArray != null && atts.XmlArray.Namespace != null) ? atts.XmlArray.Namespace : defaultNamespace;
  409. elem.MappedType = ImportListMapping (rmember.MemberType, null, elem.Namespace, atts, 0);
  410. member.ElementInfo.Add (elem);
  411. mapMember = member;
  412. }
  413. }
  414. else
  415. {
  416. // An element
  417. XmlTypeMapMemberElement member = new XmlTypeMapMemberElement ();
  418. member.ElementInfo = ImportElementInfo (rmember.MemberName, defaultNamespace, rmember.MemberType, member, atts);
  419. mapMember = member;
  420. }
  421. mapMember.DefaultValue = atts.XmlDefaultValue;
  422. mapMember.TypeData = typeData;
  423. mapMember.Name = rmember.MemberName;
  424. return mapMember;
  425. }
  426. XmlTypeMapElementInfoList ImportElementInfo (string defaultName, string defaultNamespace, Type defaultType, XmlTypeMapMemberElement member, XmlAttributes atts)
  427. {
  428. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  429. if (atts.XmlText != null)
  430. {
  431. member.IsXmlTextCollector = true;
  432. if (atts.XmlText.Type != null) defaultType = atts.XmlText.Type;
  433. if (defaultType == typeof(XmlNode)) defaultType = typeof(XmlText); // Nodes must be text nodes
  434. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  435. if (atts.XmlText.DataType != null) elem.DataType = atts.XmlText.DataType;
  436. if (elem.TypeData.SchemaType != SchemaTypes.Primitive && elem.TypeData.SchemaType != SchemaTypes.Enum &&
  437. elem.TypeData.SchemaType != SchemaTypes.XmlNode)
  438. throw new InvalidOperationException ("XmlText cannot be used to encode complex types");
  439. elem.ElementName = "<text>";
  440. elem.Namespace = string.Empty;
  441. elem.WrappedElement = false;
  442. list.Add (elem);
  443. }
  444. if (atts.XmlElements.Count == 0)
  445. {
  446. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(defaultType));
  447. elem.ElementName = defaultName;
  448. elem.Namespace = defaultNamespace;
  449. if (elem.TypeData.IsComplexType)
  450. elem.MappedType = ImportTypeMapping (defaultType, null, defaultNamespace);
  451. list.Add (elem);
  452. }
  453. bool multiType = (atts.XmlElements.Count > 1);
  454. foreach (XmlElementAttribute att in atts.XmlElements)
  455. {
  456. Type elemType = (att.Type != null) ? att.Type : defaultType;
  457. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(elemType));
  458. elem.ElementName = (att.ElementName != null) ? att.ElementName : defaultName;
  459. elem.DataType = att.DataType;
  460. elem.Namespace = (att.Namespace != null) ? att.Namespace : defaultNamespace;
  461. elem.Form = att.Form;
  462. elem.IsNullable = att.IsNullable;
  463. if (elem.TypeData.IsComplexType)
  464. elem.MappedType = ImportTypeMapping (elemType, null, elem.Namespace);
  465. if (att.ElementName != null)
  466. elem.ElementName = att.ElementName;
  467. else if (multiType) {
  468. if (elem.MappedType != null) elem.ElementName = elem.MappedType.ElementName;
  469. else elem.ElementName = TypeTranslator.GetTypeData(elemType).XmlType;
  470. }
  471. else
  472. elem.ElementName = defaultName;
  473. list.Add (elem);
  474. }
  475. return list;
  476. }
  477. XmlTypeMapElementInfoList ImportAnyElementInfo (string defaultNamespace, XmlTypeMapMemberElement member, XmlAttributes atts)
  478. {
  479. XmlTypeMapElementInfoList list = new XmlTypeMapElementInfoList();
  480. foreach (XmlAnyElementAttribute att in atts.XmlAnyElements)
  481. {
  482. XmlTypeMapElementInfo elem = new XmlTypeMapElementInfo (member, TypeTranslator.GetTypeData(typeof(XmlElement)));
  483. elem.ElementName = (att.Name != null) ? att.Name : "";
  484. elem.Namespace = (att.Namespace != null) ? att.Namespace : "";
  485. list.Add (elem);
  486. }
  487. return list;
  488. }
  489. public void IncludeType (Type type)
  490. {
  491. if (type == null)
  492. throw new ArgumentNullException ("type");
  493. if (includedTypes == null) includedTypes = new ArrayList ();
  494. includedTypes.Add (type);
  495. }
  496. #endregion // Methods
  497. }
  498. }