XmlSerializationWriter.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. //
  2. // System.Xml.Serialization.XmlSerializationWriter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.Globalization;
  33. using System.Text;
  34. using System.Xml;
  35. using System.Xml.Schema;
  36. using System.Runtime.Serialization;
  37. using System.Reflection;
  38. namespace System.Xml.Serialization
  39. {
  40. public abstract class XmlSerializationWriter
  41. #if NET_2_0
  42. : XmlSerializationGeneratedCode
  43. #endif
  44. {
  45. #region Fields
  46. ObjectIDGenerator idGenerator;
  47. int qnameCount;
  48. bool topLevelElement = false;
  49. ArrayList namespaces;
  50. XmlWriter writer;
  51. Queue referencedElements;
  52. Hashtable callbacks;
  53. Hashtable serializedObjects;
  54. const string xmlNamespace = "http://www.w3.org/2000/xmlns/";
  55. const string unexpectedTypeError = "The type {0} was not expected. Use the" +
  56. " XmlInclude or SoapInclude attribute to specify types that are not known statically.";
  57. #endregion // Fields
  58. #region Constructors
  59. protected XmlSerializationWriter ()
  60. {
  61. qnameCount = 0;
  62. serializedObjects = new Hashtable ();
  63. }
  64. internal void Initialize (XmlWriter writer, XmlSerializerNamespaces nss)
  65. {
  66. this.writer = writer;
  67. if (nss != null)
  68. {
  69. namespaces = new ArrayList ();
  70. foreach (XmlQualifiedName ns in nss.ToArray())
  71. if (ns.Name != "" && ns.Namespace != "")
  72. namespaces.Add (ns);
  73. }
  74. }
  75. #endregion // Constructors
  76. #region Properties
  77. protected ArrayList Namespaces {
  78. get { return namespaces; }
  79. set { namespaces = value; }
  80. }
  81. protected XmlWriter Writer {
  82. get { return writer; }
  83. set { writer = value; }
  84. }
  85. #endregion // Properties
  86. #region Methods
  87. protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
  88. {
  89. WriteCallbackInfo info = new WriteCallbackInfo ();
  90. info.Type = type;
  91. info.TypeName = typeName;
  92. info.TypeNs = typeNs;
  93. info.Callback = callback;
  94. if (callbacks == null) callbacks = new Hashtable ();
  95. callbacks.Add (type, info);
  96. }
  97. protected Exception CreateChoiceIdentifierValueException (string value, string identifier, string name, string ns)
  98. {
  99. string message = string.Format ("Value '{0}' of the choice"
  100. + " identifier '{1}' does not match element '{2}'"
  101. + " from namespace '{3}'.", value, identifier,
  102. name, ns);
  103. return new InvalidOperationException (message);
  104. }
  105. protected Exception CreateInvalidChoiceIdentifierValueException (string type, string identifier)
  106. {
  107. string message = string.Format ("Invalid or missing choice"
  108. + " identifier '{0}' of type '{1}'.", identifier,
  109. type);
  110. return new InvalidOperationException (message);
  111. }
  112. protected Exception CreateMismatchChoiceException (string value, string elementName, string enumValue)
  113. {
  114. string message = String.Format ("Value of {0} mismatches the type of {1}, you need to set it to {2}.", elementName, value, enumValue);
  115. return new InvalidOperationException (message);
  116. }
  117. protected Exception CreateUnknownAnyElementException (string name, string ns)
  118. {
  119. string message = String.Format ("The XML element named '{0}' from namespace '{1}' was not expected. The XML element name and namespace must match those provided via XmlAnyElementAttribute(s).", name, ns);
  120. return new InvalidOperationException (message);
  121. }
  122. protected Exception CreateUnknownTypeException (object o)
  123. {
  124. return CreateUnknownTypeException (o.GetType ());
  125. }
  126. protected Exception CreateUnknownTypeException (Type type)
  127. {
  128. string message = String.Format ("The type {0} may not be used in this context.", type);
  129. return new InvalidOperationException (message);
  130. }
  131. protected static byte[] FromByteArrayBase64 (byte[] value)
  132. {
  133. return value;
  134. }
  135. protected static string FromByteArrayHex (byte[] value)
  136. {
  137. return XmlCustomFormatter.FromByteArrayHex (value);
  138. }
  139. protected static string FromChar (char value)
  140. {
  141. return XmlCustomFormatter.FromChar (value);
  142. }
  143. protected static string FromDate (DateTime value)
  144. {
  145. return XmlCustomFormatter.FromDate (value);
  146. }
  147. protected static string FromDateTime (DateTime value)
  148. {
  149. return XmlCustomFormatter.FromDateTime (value);
  150. }
  151. protected static string FromEnum (long value, string[] values, long[] ids)
  152. {
  153. return XmlCustomFormatter.FromEnum (value, values, ids);
  154. }
  155. protected static string FromTime (DateTime value)
  156. {
  157. return XmlCustomFormatter.FromTime (value);
  158. }
  159. protected static string FromXmlName (string name)
  160. {
  161. return XmlCustomFormatter.FromXmlName (name);
  162. }
  163. protected static string FromXmlNCName (string ncName)
  164. {
  165. return XmlCustomFormatter.FromXmlNCName (ncName);
  166. }
  167. protected static string FromXmlNmToken (string nmToken)
  168. {
  169. return XmlCustomFormatter.FromXmlNmToken (nmToken);
  170. }
  171. protected static string FromXmlNmTokens (string nmTokens)
  172. {
  173. return XmlCustomFormatter.FromXmlNmTokens (nmTokens);
  174. }
  175. protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName)
  176. {
  177. if (xmlQualifiedName == null || xmlQualifiedName == XmlQualifiedName.Empty)
  178. return null;
  179. return GetQualifiedName (xmlQualifiedName.Name, xmlQualifiedName.Namespace);
  180. }
  181. private string GetId (object o, bool addToReferencesList)
  182. {
  183. if (idGenerator == null) idGenerator = new ObjectIDGenerator ();
  184. bool firstTime;
  185. long lid = idGenerator.GetId (o, out firstTime);
  186. return String.Format (CultureInfo.InvariantCulture, "id{0}", lid);
  187. }
  188. bool AlreadyQueued (object ob)
  189. {
  190. if (idGenerator == null) return false;
  191. bool firstTime;
  192. idGenerator.HasId (ob, out firstTime);
  193. return !firstTime;
  194. }
  195. private string GetNamespacePrefix (string ns)
  196. {
  197. string prefix = Writer.LookupPrefix (ns);
  198. if (prefix == null)
  199. {
  200. prefix = String.Format (CultureInfo.InvariantCulture, "q{0}", ++qnameCount);
  201. WriteAttribute ("xmlns", prefix, null, ns);
  202. }
  203. return prefix;
  204. }
  205. private string GetQualifiedName (string name, string ns)
  206. {
  207. if (ns == String.Empty) return name;
  208. string prefix = GetNamespacePrefix (ns);
  209. if (prefix == String.Empty)
  210. return name;
  211. else
  212. return String.Format ("{0}:{1}", prefix, name);
  213. }
  214. protected abstract void InitCallbacks ();
  215. protected void TopLevelElement ()
  216. {
  217. topLevelElement = true;
  218. }
  219. protected void WriteAttribute (string localName, byte[] value)
  220. {
  221. WriteAttribute (localName, String.Empty, value);
  222. }
  223. protected void WriteAttribute (string localName, string value)
  224. {
  225. WriteAttribute (String.Empty, localName, String.Empty, value);
  226. }
  227. protected void WriteAttribute (string localName, string ns, byte[] value)
  228. {
  229. if (value == null)
  230. return;
  231. Writer.WriteStartAttribute (localName, ns);
  232. WriteValue (value);
  233. Writer.WriteEndAttribute ();
  234. }
  235. protected void WriteAttribute (string localName, string ns, string value)
  236. {
  237. WriteAttribute (null, localName, ns, value);
  238. }
  239. protected void WriteAttribute (string prefix, string localName, string ns, string value)
  240. {
  241. if (value == null)
  242. return;
  243. Writer.WriteAttributeString (prefix, localName, ns, value);
  244. }
  245. protected void WriteElementEncoded (XmlNode node, string name, string ns, bool isNullable, bool any)
  246. {
  247. if (name != string.Empty)
  248. {
  249. if (node == null)
  250. {
  251. if (isNullable)
  252. WriteNullTagEncoded (name, ns);
  253. }
  254. else
  255. {
  256. Writer.WriteStartElement (name, ns);
  257. node.WriteTo (Writer);
  258. Writer.WriteEndElement ();
  259. }
  260. }
  261. else
  262. node.WriteTo (Writer);
  263. }
  264. protected void WriteElementLiteral (XmlNode node, string name, string ns, bool isNullable, bool any)
  265. {
  266. if (name != string.Empty)
  267. {
  268. if (node == null)
  269. {
  270. if (isNullable)
  271. WriteNullTagLiteral (name, ns);
  272. }
  273. else
  274. {
  275. Writer.WriteStartElement (name, ns);
  276. node.WriteTo (Writer);
  277. Writer.WriteEndElement ();
  278. }
  279. }
  280. else
  281. node.WriteTo (Writer);
  282. }
  283. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value)
  284. {
  285. WriteElementQualifiedName (localName, String.Empty, value, null);
  286. }
  287. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value)
  288. {
  289. WriteElementQualifiedName (localName, ns, value, null);
  290. }
  291. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value, XmlQualifiedName xsiType)
  292. {
  293. WriteElementQualifiedName (localName, String.Empty, value, xsiType);
  294. }
  295. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  296. {
  297. localName = XmlCustomFormatter.FromXmlNCName (localName);
  298. WriteStartElement (localName, ns);
  299. if (xsiType != null) WriteXsiType (xsiType.Name, xsiType.Namespace);
  300. Writer.WriteString (FromXmlQualifiedName (value));
  301. WriteEndElement ();
  302. }
  303. protected void WriteElementString (string localName, string value)
  304. {
  305. WriteElementString (localName, String.Empty, value, null);
  306. }
  307. protected void WriteElementString (string localName, string ns, string value)
  308. {
  309. WriteElementString (localName, ns, value, null);
  310. }
  311. protected void WriteElementString (string localName, string value, XmlQualifiedName xsiType)
  312. {
  313. WriteElementString (localName, String.Empty, value, xsiType);
  314. }
  315. protected void WriteElementString (string localName, string ns, string value, XmlQualifiedName xsiType)
  316. {
  317. if (value == null) return;
  318. if (xsiType != null) {
  319. localName = XmlCustomFormatter.FromXmlNCName (localName);
  320. WriteStartElement (localName, ns);
  321. WriteXsiType (xsiType.Name, xsiType.Namespace);
  322. Writer.WriteString (value);
  323. WriteEndElement ();
  324. }
  325. else
  326. Writer.WriteElementString (localName, ns, value);
  327. }
  328. protected void WriteElementStringRaw (string localName, byte[] value)
  329. {
  330. WriteElementStringRaw (localName, String.Empty, value, null);
  331. }
  332. protected void WriteElementStringRaw (string localName, string value)
  333. {
  334. WriteElementStringRaw (localName, String.Empty, value, null);
  335. }
  336. protected void WriteElementStringRaw (string localName, byte[] value, XmlQualifiedName xsiType)
  337. {
  338. WriteElementStringRaw (localName, String.Empty, value, xsiType);
  339. }
  340. protected void WriteElementStringRaw (string localName, string ns, byte[] value)
  341. {
  342. WriteElementStringRaw (localName, ns, value, null);
  343. }
  344. protected void WriteElementStringRaw (string localName, string ns, string value)
  345. {
  346. WriteElementStringRaw (localName, ns, value, null);
  347. }
  348. protected void WriteElementStringRaw (string localName, string value, XmlQualifiedName xsiType)
  349. {
  350. WriteElementStringRaw (localName, String.Empty, value, null);
  351. }
  352. protected void WriteElementStringRaw (string localName, string ns, byte[] value, XmlQualifiedName xsiType)
  353. {
  354. if (value == null)
  355. return;
  356. WriteStartElement (localName, ns);
  357. if (xsiType != null)
  358. WriteXsiType (xsiType.Name, xsiType.Namespace);
  359. if (value.Length > 0)
  360. Writer.WriteBase64(value,0,value.Length);
  361. WriteEndElement ();
  362. }
  363. protected void WriteElementStringRaw (string localName, string ns, string value, XmlQualifiedName xsiType)
  364. {
  365. localName = XmlCustomFormatter.FromXmlNCName (localName);
  366. WriteStartElement (localName, ns);
  367. if (xsiType != null)
  368. WriteXsiType (xsiType.Name, xsiType.Namespace);
  369. Writer.WriteRaw (value);
  370. WriteEndElement ();
  371. }
  372. protected void WriteEmptyTag (string name)
  373. {
  374. WriteEmptyTag (name, String.Empty);
  375. }
  376. protected void WriteEmptyTag (string name, string ns)
  377. {
  378. name = XmlCustomFormatter.FromXmlName (name);
  379. WriteStartElement (name, ns);
  380. WriteEndElement ();
  381. }
  382. protected void WriteEndElement ()
  383. {
  384. WriteEndElement (null);
  385. }
  386. protected void WriteEndElement (object o)
  387. {
  388. if (o != null)
  389. serializedObjects.Remove (o);
  390. Writer.WriteEndElement ();
  391. }
  392. protected void WriteId (object o)
  393. {
  394. WriteAttribute ("id", GetId (o, true));
  395. }
  396. protected void WriteNamespaceDeclarations (XmlSerializerNamespaces ns)
  397. {
  398. if (ns == null)
  399. return;
  400. ICollection namespaces = ns.Namespaces.Values;
  401. foreach (XmlQualifiedName qn in namespaces) {
  402. if (qn.Namespace != String.Empty && Writer.LookupPrefix (qn.Namespace) != qn.Name)
  403. WriteAttribute ("xmlns", qn.Name, xmlNamespace, qn.Namespace);
  404. }
  405. }
  406. protected void WriteNullableQualifiedNameEncoded (string name, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  407. {
  408. if (value != null)
  409. WriteElementQualifiedName (name, ns, value, xsiType);
  410. else
  411. WriteNullTagEncoded (name, ns);
  412. }
  413. protected void WriteNullableQualifiedNameLiteral (string name, string ns, XmlQualifiedName value)
  414. {
  415. if (value != null)
  416. WriteElementQualifiedName (name, ns, value);
  417. else
  418. WriteNullTagLiteral (name, ns);
  419. }
  420. protected void WriteNullableStringEncoded (string name, string ns, string value, XmlQualifiedName xsiType)
  421. {
  422. if (value != null)
  423. WriteElementString (name, ns, value, xsiType);
  424. else
  425. WriteNullTagEncoded (name, ns);
  426. }
  427. protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
  428. {
  429. if (value == null)
  430. WriteNullTagEncoded (name, ns);
  431. else
  432. WriteElementStringRaw (name, ns, value, xsiType);
  433. }
  434. protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
  435. {
  436. if (value == null)
  437. WriteNullTagEncoded (name, ns);
  438. else
  439. WriteElementStringRaw (name, ns, value, xsiType);
  440. }
  441. protected void WriteNullableStringLiteral (string name, string ns, string value)
  442. {
  443. if (value != null)
  444. WriteElementString (name, ns, value, null);
  445. else
  446. WriteNullTagLiteral (name, ns);
  447. }
  448. protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
  449. {
  450. if (value == null)
  451. WriteNullTagLiteral (name, ns);
  452. else
  453. WriteElementStringRaw (name, ns, value);
  454. }
  455. protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
  456. {
  457. if (value == null)
  458. WriteNullTagLiteral (name, ns);
  459. else
  460. WriteElementStringRaw (name, ns, value);
  461. }
  462. protected void WriteNullTagEncoded (string name)
  463. {
  464. WriteNullTagEncoded (name, String.Empty);
  465. }
  466. protected void WriteNullTagEncoded (string name, string ns)
  467. {
  468. Writer.WriteStartElement (name, ns);
  469. #if NET_1_1
  470. Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
  471. #else
  472. Writer.WriteAttributeString ("null", XmlSchema.InstanceNamespace, "1");
  473. #endif
  474. Writer.WriteEndElement ();
  475. }
  476. protected void WriteNullTagLiteral (string name)
  477. {
  478. WriteNullTagLiteral (name, String.Empty);
  479. }
  480. protected void WriteNullTagLiteral (string name, string ns)
  481. {
  482. WriteStartElement (name, ns);
  483. Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
  484. WriteEndElement ();
  485. }
  486. protected void WritePotentiallyReferencingElement (string n, string ns, object o)
  487. {
  488. WritePotentiallyReferencingElement (n, ns, o, null, false, false);
  489. }
  490. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
  491. {
  492. WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
  493. }
  494. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
  495. {
  496. WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
  497. }
  498. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
  499. {
  500. if (o == null)
  501. {
  502. if (isNullable) WriteNullTagEncoded (n, ns);
  503. return;
  504. }
  505. WriteStartElement (n, ns, true);
  506. CheckReferenceQueue ();
  507. if (callbacks.ContainsKey (o.GetType ()))
  508. {
  509. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  510. if (o.GetType ().IsEnum) {
  511. info.Callback (o);
  512. }
  513. else if (suppressReference) {
  514. Writer.WriteAttributeString ("id", GetId (o, false));
  515. if (ambientType != o.GetType ()) WriteXsiType(info.TypeName, info.TypeNs);
  516. info.Callback (o);
  517. }
  518. else {
  519. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  520. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  521. }
  522. }
  523. else
  524. {
  525. // Must be a primitive type or array of primitives
  526. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  527. if (td.SchemaType == SchemaTypes.Primitive) {
  528. WriteXsiType (td.XmlType, XmlSchema.Namespace);
  529. Writer.WriteString (XmlCustomFormatter.ToXmlString (td, o));
  530. } else if (IsPrimitiveArray (td)) {
  531. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  532. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  533. } else
  534. throw new InvalidOperationException ("Invalid type: " + o.GetType().FullName);
  535. }
  536. WriteEndElement ();
  537. }
  538. protected void WriteReferencedElements ()
  539. {
  540. if (referencedElements == null) return;
  541. if (callbacks == null) return;
  542. while (referencedElements.Count > 0)
  543. {
  544. object o = referencedElements.Dequeue ();
  545. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  546. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  547. if (info != null) {
  548. WriteStartElement (info.TypeName, info.TypeNs, true);
  549. Writer.WriteAttributeString ("id", GetId (o, false));
  550. if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute
  551. WriteXsiType(info.TypeName, info.TypeNs);
  552. info.Callback (o);
  553. WriteEndElement ();
  554. } else if (IsPrimitiveArray (td)) {
  555. WriteArray (o, td);
  556. }
  557. }
  558. }
  559. bool IsPrimitiveArray (TypeData td)
  560. {
  561. if (td.SchemaType == SchemaTypes.Array) {
  562. if (td.ListItemTypeData.SchemaType == SchemaTypes.Primitive || td.ListItemType == typeof(object))
  563. return true;
  564. return IsPrimitiveArray (td.ListItemTypeData);
  565. } else
  566. return false;
  567. }
  568. void WriteArray (object o, TypeData td)
  569. {
  570. TypeData itemTypeData = td;
  571. string xmlType;
  572. int nDims = -1;
  573. do {
  574. itemTypeData = itemTypeData.ListItemTypeData;
  575. xmlType = itemTypeData.XmlType;
  576. nDims++;
  577. }
  578. while (itemTypeData.SchemaType == SchemaTypes.Array );
  579. while (nDims-- > 0)
  580. xmlType += "[]";
  581. WriteStartElement("Array", XmlSerializer.EncodingNamespace, true);
  582. Writer.WriteAttributeString("id", GetId(o, false));
  583. if (td.SchemaType == SchemaTypes.Array) {
  584. Array a = (Array)o;
  585. int len = a.Length;
  586. Writer.WriteAttributeString("arrayType", XmlSerializer.EncodingNamespace, GetQualifiedName(xmlType, XmlSchema.Namespace) + "[" + len.ToString() + "]");
  587. for (int i = 0; i < len; i++) {
  588. WritePotentiallyReferencingElement("Item", "", a.GetValue(i), td.ListItemType, false, true);
  589. }
  590. }
  591. WriteEndElement();
  592. }
  593. protected void WriteReferencingElement (string n, string ns, object o)
  594. {
  595. WriteReferencingElement (n, ns, o, false);
  596. }
  597. protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
  598. {
  599. if (o == null)
  600. {
  601. if (isNullable) WriteNullTagEncoded (n, ns);
  602. return;
  603. }
  604. else
  605. {
  606. CheckReferenceQueue ();
  607. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  608. Writer.WriteStartElement (n, ns);
  609. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  610. Writer.WriteEndElement ();
  611. }
  612. }
  613. void CheckReferenceQueue ()
  614. {
  615. if (referencedElements == null)
  616. {
  617. referencedElements = new Queue ();
  618. InitCallbacks ();
  619. }
  620. }
  621. [MonoTODO]
  622. protected void WriteRpcResult (string name, string ns)
  623. {
  624. throw new NotImplementedException ();
  625. }
  626. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
  627. {
  628. if (serializable == null)
  629. {
  630. if (isNullable) WriteNullTagLiteral (name, ns);
  631. return;
  632. }
  633. else
  634. {
  635. Writer.WriteStartElement (name, ns);
  636. serializable.WriteXml (Writer);
  637. Writer.WriteEndElement ();
  638. }
  639. }
  640. protected void WriteStartDocument ()
  641. {
  642. if (Writer.WriteState == WriteState.Start)
  643. Writer.WriteStartDocument ();
  644. }
  645. protected void WriteStartElement (string name)
  646. {
  647. WriteStartElement (name, String.Empty, null, false);
  648. }
  649. protected void WriteStartElement (string name, string ns)
  650. {
  651. WriteStartElement (name, ns, null, false);
  652. }
  653. protected void WriteStartElement (string name, string ns, bool writePrefixed)
  654. {
  655. WriteStartElement (name, ns, null, writePrefixed);
  656. }
  657. protected void WriteStartElement (string name, string ns, object o)
  658. {
  659. WriteStartElement (name, ns, o, false);
  660. }
  661. protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
  662. {
  663. if (o != null)
  664. {
  665. if (serializedObjects.Contains (o))
  666. throw new InvalidOperationException ("A cirtular reference was detected while serializing an object of type " + o.GetType().Name);
  667. else
  668. serializedObjects [o] = o;
  669. }
  670. string prefix = null;
  671. if (topLevelElement && ns != null && ns.Length != 0)
  672. {
  673. foreach (XmlQualifiedName qn in namespaces)
  674. if (qn.Namespace == ns) {
  675. prefix = qn.Name;
  676. writePrefixed = true;
  677. break;
  678. }
  679. }
  680. if (writePrefixed && ns != string.Empty)
  681. {
  682. name = XmlCustomFormatter.FromXmlName (name);
  683. if (prefix == null)
  684. prefix = Writer.LookupPrefix (ns);
  685. if (prefix == null || prefix.Length == 0)
  686. prefix = "q" + (++qnameCount);
  687. Writer.WriteStartElement (prefix, name, ns);
  688. } else
  689. Writer.WriteStartElement (name, ns);
  690. if (topLevelElement)
  691. {
  692. if (namespaces != null) {
  693. foreach (XmlQualifiedName qn in namespaces)
  694. {
  695. string currentPrefix = Writer.LookupPrefix (qn.Namespace);
  696. if (currentPrefix != null && currentPrefix.Length != 0) continue;
  697. WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
  698. }
  699. }
  700. topLevelElement = false;
  701. }
  702. }
  703. protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
  704. {
  705. string value;
  706. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  707. name = XmlCustomFormatter.FromXmlName (name);
  708. Writer.WriteStartElement (name, ns);
  709. if (o is XmlNode[]) {
  710. foreach (XmlNode node in (XmlNode[])o)
  711. node.WriteTo (Writer);
  712. }
  713. else {
  714. if (o is XmlQualifiedName)
  715. value = FromXmlQualifiedName ((XmlQualifiedName) o);
  716. else
  717. value = XmlCustomFormatter.ToXmlString (td, o);
  718. if (xsiType)
  719. {
  720. if (td.SchemaType != SchemaTypes.Primitive)
  721. throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
  722. WriteXsiType (td.XmlType, XmlSchema.Namespace);
  723. }
  724. WriteValue (value);
  725. }
  726. Writer.WriteEndElement ();
  727. }
  728. protected void WriteValue (byte[] value)
  729. {
  730. Writer.WriteBase64 (value, 0, value.Length);
  731. }
  732. protected void WriteValue (string value)
  733. {
  734. if (value != null)
  735. Writer.WriteString (value);
  736. }
  737. protected void WriteXmlAttribute (XmlNode node)
  738. {
  739. WriteXmlAttribute (node, null);
  740. }
  741. protected void WriteXmlAttribute (XmlNode node, object container)
  742. {
  743. XmlAttribute attr = node as XmlAttribute;
  744. if (attr == null)
  745. throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
  746. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace)
  747. {
  748. // The wsdl arrayType attribute needs special handling
  749. if (attr.LocalName == "arrayType") {
  750. string ns, type, dimensions;
  751. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  752. string value = GetQualifiedName (type + dimensions, ns);
  753. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, value);
  754. return;
  755. }
  756. }
  757. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
  758. }
  759. protected void WriteXsiType (string name, string ns)
  760. {
  761. if (ns != null && ns != string.Empty)
  762. WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
  763. else
  764. WriteAttribute ("type", XmlSchema.InstanceNamespace, name);
  765. }
  766. #if NET_2_0
  767. [MonoTODO]
  768. protected Exception CreateInvalidAnyTypeException (object o)
  769. {
  770. throw new NotImplementedException ();
  771. }
  772. [MonoTODO]
  773. protected Exception CreateInvalidAnyTypeException (Type t)
  774. {
  775. throw new NotImplementedException ();
  776. }
  777. protected Exception CreateInvalidEnumValueException (object value, string typeName)
  778. {
  779. return new InvalidOperationException (string.Format(CultureInfo.CurrentCulture,
  780. "'{0}' is not a valid value for {1}.", value, typeName));
  781. }
  782. protected static string FromEnum (long value, string[] values, long[] ids, string typeName)
  783. {
  784. return XmlCustomFormatter.FromEnum (value, values, ids, typeName);
  785. }
  786. [MonoTODO]
  787. protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName, bool ignoreEmpty)
  788. {
  789. throw new NotImplementedException ();
  790. }
  791. [MonoTODO]
  792. protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
  793. {
  794. throw new NotImplementedException ();
  795. }
  796. [MonoTODO]
  797. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable, bool any)
  798. {
  799. throw new NotImplementedException ();
  800. }
  801. [MonoTODO]
  802. protected bool EscapeName
  803. {
  804. get { throw new NotImplementedException(); }
  805. set { throw new NotImplementedException(); }
  806. }
  807. #endif
  808. #endregion
  809. class WriteCallbackInfo
  810. {
  811. public Type Type;
  812. public string TypeName;
  813. public string TypeNs;
  814. public XmlSerializationWriteCallback Callback;
  815. }
  816. }
  817. }