XmlReflectionImporter.cs 23 KB

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