XmlSerializationWriter.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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
  526. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  527. if (td.SchemaType != SchemaTypes.Primitive)
  528. throw new InvalidOperationException ("Invalid type: " + o.GetType().FullName);
  529. WriteXsiType(td.XmlType, XmlSchema.Namespace);
  530. Writer.WriteString (XmlCustomFormatter.ToXmlString (td, o));
  531. }
  532. WriteEndElement ();
  533. }
  534. protected void WriteReferencedElements ()
  535. {
  536. if (referencedElements == null) return;
  537. if (callbacks == null) return;
  538. while (referencedElements.Count > 0)
  539. {
  540. object o = referencedElements.Dequeue ();
  541. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  542. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  543. WriteStartElement (info.TypeName, info.TypeNs, true);
  544. Writer.WriteAttributeString ("id", GetId (o, false));
  545. if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute
  546. WriteXsiType(info.TypeName, info.TypeNs);
  547. info.Callback (o);
  548. WriteEndElement ();
  549. }
  550. }
  551. protected void WriteReferencingElement (string n, string ns, object o)
  552. {
  553. WriteReferencingElement (n, ns, o, false);
  554. }
  555. protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
  556. {
  557. if (o == null)
  558. {
  559. if (isNullable) WriteNullTagEncoded (n, ns);
  560. return;
  561. }
  562. else
  563. {
  564. CheckReferenceQueue ();
  565. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  566. Writer.WriteStartElement (n, ns);
  567. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  568. Writer.WriteEndElement ();
  569. }
  570. }
  571. void CheckReferenceQueue ()
  572. {
  573. if (referencedElements == null)
  574. {
  575. referencedElements = new Queue ();
  576. InitCallbacks ();
  577. }
  578. }
  579. [MonoTODO]
  580. protected void WriteRpcResult (string name, string ns)
  581. {
  582. throw new NotImplementedException ();
  583. }
  584. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
  585. {
  586. if (serializable == null)
  587. {
  588. if (isNullable) WriteNullTagLiteral (name, ns);
  589. return;
  590. }
  591. else
  592. {
  593. Writer.WriteStartElement (name, ns);
  594. serializable.WriteXml (Writer);
  595. Writer.WriteEndElement ();
  596. }
  597. }
  598. protected void WriteStartDocument ()
  599. {
  600. if (Writer.WriteState == WriteState.Start)
  601. Writer.WriteStartDocument ();
  602. }
  603. protected void WriteStartElement (string name)
  604. {
  605. WriteStartElement (name, String.Empty, null, false);
  606. }
  607. protected void WriteStartElement (string name, string ns)
  608. {
  609. WriteStartElement (name, ns, null, false);
  610. }
  611. protected void WriteStartElement (string name, string ns, bool writePrefixed)
  612. {
  613. WriteStartElement (name, ns, null, writePrefixed);
  614. }
  615. protected void WriteStartElement (string name, string ns, object o)
  616. {
  617. WriteStartElement (name, ns, o, false);
  618. }
  619. protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
  620. {
  621. if (o != null)
  622. {
  623. if (serializedObjects.Contains (o))
  624. throw new InvalidOperationException ("A cirtular reference was detected while serializing an object of type " + o.GetType().Name);
  625. else
  626. serializedObjects [o] = o;
  627. }
  628. string prefix = null;
  629. if (topLevelElement && ns != null && ns.Length != 0)
  630. {
  631. foreach (XmlQualifiedName qn in namespaces)
  632. if (qn.Namespace == ns) {
  633. prefix = qn.Name;
  634. writePrefixed = true;
  635. break;
  636. }
  637. }
  638. if (writePrefixed && ns != string.Empty)
  639. {
  640. name = XmlCustomFormatter.FromXmlName (name);
  641. if (prefix == null)
  642. prefix = Writer.LookupPrefix (ns);
  643. if (prefix == null || prefix.Length == 0)
  644. prefix = "q" + (++qnameCount);
  645. Writer.WriteStartElement (prefix, name, ns);
  646. } else
  647. Writer.WriteStartElement (name, ns);
  648. if (topLevelElement)
  649. {
  650. if (namespaces != null) {
  651. foreach (XmlQualifiedName qn in namespaces)
  652. {
  653. string currentPrefix = Writer.LookupPrefix (qn.Namespace);
  654. if (currentPrefix != null && currentPrefix.Length != 0) continue;
  655. WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
  656. }
  657. }
  658. topLevelElement = false;
  659. }
  660. }
  661. protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
  662. {
  663. string value;
  664. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  665. name = XmlCustomFormatter.FromXmlName (name);
  666. Writer.WriteStartElement (name, ns);
  667. if (o is XmlQualifiedName)
  668. value = FromXmlQualifiedName ((XmlQualifiedName) o);
  669. else
  670. value = XmlCustomFormatter.ToXmlString (td, o);
  671. if (xsiType)
  672. {
  673. if (td.SchemaType != SchemaTypes.Primitive)
  674. throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
  675. WriteXsiType (td.XmlType, XmlSchema.Namespace);
  676. }
  677. WriteValue (value);
  678. Writer.WriteEndElement ();
  679. }
  680. protected void WriteValue (byte[] value)
  681. {
  682. Writer.WriteBase64 (value, 0, value.Length);
  683. }
  684. protected void WriteValue (string value)
  685. {
  686. if (value != null)
  687. Writer.WriteString (value);
  688. }
  689. protected void WriteXmlAttribute (XmlNode node)
  690. {
  691. WriteXmlAttribute (node, null);
  692. }
  693. protected void WriteXmlAttribute (XmlNode node, object container)
  694. {
  695. XmlAttribute attr = node as XmlAttribute;
  696. if (attr == null)
  697. throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
  698. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace)
  699. {
  700. // The wsdl arrayType attribute needs special handling
  701. if (attr.LocalName == "arrayType") {
  702. string ns, type, dimensions;
  703. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  704. string value = GetQualifiedName (type + dimensions, ns);
  705. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, value);
  706. return;
  707. }
  708. }
  709. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
  710. }
  711. protected void WriteXsiType (string name, string ns)
  712. {
  713. if (ns != null && ns != string.Empty)
  714. WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
  715. else
  716. WriteAttribute ("type", XmlSchema.InstanceNamespace, name);
  717. }
  718. #if NET_2_0
  719. [MonoTODO]
  720. protected Exception CreateInvalidAnyTypeException (object o)
  721. {
  722. throw new NotImplementedException ();
  723. }
  724. [MonoTODO]
  725. protected Exception CreateInvalidAnyTypeException (Type t)
  726. {
  727. throw new NotImplementedException ();
  728. }
  729. [MonoTODO]
  730. protected Exception CreateInvalidEnumValueException (object value, string typeName)
  731. {
  732. throw new NotImplementedException ();
  733. }
  734. [MonoTODO]
  735. protected static string FromEnum (long value, string[] values, long[] ids, string typeName)
  736. {
  737. throw new NotImplementedException ();
  738. }
  739. [MonoTODO]
  740. protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName, bool ignoreEmpty)
  741. {
  742. throw new NotImplementedException ();
  743. }
  744. [MonoTODO]
  745. protected static Assembly ResolveDynamicAssembly (string assemblyFullName)
  746. {
  747. throw new NotImplementedException ();
  748. }
  749. [MonoTODO]
  750. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable, bool any)
  751. {
  752. throw new NotImplementedException ();
  753. }
  754. [MonoTODO]
  755. protected bool EscapeName
  756. {
  757. get { throw new NotImplementedException(); }
  758. set { throw new NotImplementedException(); }
  759. }
  760. #endif
  761. #endregion
  762. class WriteCallbackInfo
  763. {
  764. public Type Type;
  765. public string TypeName;
  766. public string TypeNs;
  767. public XmlSerializationWriteCallback Callback;
  768. }
  769. }
  770. }