XmlReflectionImporter.cs 23 KB

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