XmlReflectionImporter.cs 21 KB

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