XmlSerializationWriter.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  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. using System;
  11. using System.Collections;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Xml.Schema;
  15. using System.Runtime.Serialization;
  16. namespace System.Xml.Serialization {
  17. public abstract class XmlSerializationWriter {
  18. #region Fields
  19. ObjectIDGenerator idGenerator;
  20. int qnameCount;
  21. bool topLevelElement = false;
  22. ArrayList namespaces;
  23. XmlWriter writer;
  24. Queue referencedElements;
  25. Hashtable callbacks;
  26. Hashtable serializedObjects;
  27. const string xmlNamespace = "http://www.w3.org/2000/xmlns/";
  28. const string unexpectedTypeError = "The type {0} was not expected. Use the" +
  29. " XmlInclude or SoapInclude attribute to specify types that are not known statically.";
  30. #endregion // Fields
  31. #region Constructors
  32. protected XmlSerializationWriter ()
  33. {
  34. qnameCount = 0;
  35. serializedObjects = new Hashtable ();
  36. }
  37. internal void Initialize (XmlWriter writer, XmlSerializerNamespaces nss)
  38. {
  39. this.writer = writer;
  40. if (nss != null)
  41. {
  42. namespaces = new ArrayList ();
  43. foreach (XmlQualifiedName ns in nss.ToArray())
  44. if (ns.Name != "")
  45. namespaces.Add (ns);
  46. }
  47. }
  48. #endregion // Constructors
  49. #region Properties
  50. protected ArrayList Namespaces {
  51. get { return namespaces; }
  52. set { namespaces = value; }
  53. }
  54. protected XmlWriter Writer {
  55. get { return writer; }
  56. set { writer = value; }
  57. }
  58. #endregion // Properties
  59. #region Methods
  60. protected void AddWriteCallback (Type type, string typeName, string typeNs, XmlSerializationWriteCallback callback)
  61. {
  62. WriteCallbackInfo info = new WriteCallbackInfo ();
  63. info.Type = type;
  64. info.TypeName = typeName;
  65. info.TypeNs = typeNs;
  66. info.Callback = callback;
  67. if (callbacks == null) callbacks = new Hashtable ();
  68. callbacks.Add (type, info);
  69. }
  70. protected Exception CreateChoiceIdentifierValueException (string value, string identifier, string name, string ns)
  71. {
  72. string message = string.Format ("Value '{0}' of the choice"
  73. + " identifier '{1}' does not match element '{2}'"
  74. + " from namespace '{3}'.", value, identifier,
  75. name, ns);
  76. return new InvalidOperationException (message);
  77. }
  78. protected Exception CreateInvalidChoiceIdentifierValueException (string type, string identifier)
  79. {
  80. string message = string.Format ("Invalid or missing choice"
  81. + " identifier '{0}' of type '{1}'.", identifier,
  82. type);
  83. return new InvalidOperationException (message);
  84. }
  85. protected Exception CreateMismatchChoiceException (string value, string elementName, string enumValue)
  86. {
  87. string message = String.Format ("Value of {0} mismatches the type of {1}, you need to set it to {2}.", elementName, value, enumValue);
  88. return new InvalidOperationException (message);
  89. }
  90. protected Exception CreateUnknownAnyElementException (string name, string ns)
  91. {
  92. 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);
  93. return new InvalidOperationException (message);
  94. }
  95. protected Exception CreateUnknownTypeException (object o)
  96. {
  97. return CreateUnknownTypeException (o.GetType ());
  98. }
  99. protected Exception CreateUnknownTypeException (Type type)
  100. {
  101. string message = String.Format ("The type {0} may not be used in this context.", type);
  102. return new InvalidOperationException (message);
  103. }
  104. protected static byte[] FromByteArrayBase64 (byte[] value)
  105. {
  106. return value;
  107. }
  108. protected static string FromByteArrayHex (byte[] value)
  109. {
  110. return XmlCustomFormatter.FromByteArrayHex (value);
  111. }
  112. protected static string FromChar (char value)
  113. {
  114. return XmlCustomFormatter.FromChar (value);
  115. }
  116. protected static string FromDate (DateTime value)
  117. {
  118. return XmlCustomFormatter.FromDate (value);
  119. }
  120. protected static string FromDateTime (DateTime value)
  121. {
  122. return XmlCustomFormatter.FromDateTime (value);
  123. }
  124. protected static string FromEnum (long value, string[] values, long[] ids)
  125. {
  126. return XmlCustomFormatter.FromEnum (value, values, ids);
  127. }
  128. protected static string FromTime (DateTime value)
  129. {
  130. return XmlCustomFormatter.FromTime (value);
  131. }
  132. protected static string FromXmlName (string name)
  133. {
  134. return XmlCustomFormatter.FromXmlName (name);
  135. }
  136. protected static string FromXmlNCName (string ncName)
  137. {
  138. return XmlCustomFormatter.FromXmlNCName (ncName);
  139. }
  140. protected static string FromXmlNmToken (string nmToken)
  141. {
  142. return XmlCustomFormatter.FromXmlNmToken (nmToken);
  143. }
  144. protected static string FromXmlNmTokens (string nmTokens)
  145. {
  146. return XmlCustomFormatter.FromXmlNmTokens (nmTokens);
  147. }
  148. protected string FromXmlQualifiedName (XmlQualifiedName xmlQualifiedName)
  149. {
  150. if (xmlQualifiedName == null || xmlQualifiedName == XmlQualifiedName.Empty)
  151. return null;
  152. return GetQualifiedName (xmlQualifiedName.Name, xmlQualifiedName.Namespace);
  153. }
  154. private string GetId (object o, bool addToReferencesList)
  155. {
  156. if (idGenerator == null) idGenerator = new ObjectIDGenerator ();
  157. bool firstTime;
  158. long lid = idGenerator.GetId (o, out firstTime);
  159. return String.Format ("id{0}", lid);
  160. }
  161. bool AlreadyQueued (object ob)
  162. {
  163. if (idGenerator == null) return false;
  164. bool firstTime;
  165. idGenerator.HasId (ob, out firstTime);
  166. return !firstTime;
  167. }
  168. private string GetNamespacePrefix (string ns)
  169. {
  170. string prefix = Writer.LookupPrefix (ns);
  171. if (prefix == null)
  172. {
  173. prefix = String.Format ("q{0}", ++qnameCount);
  174. WriteAttribute ("xmlns", prefix, null, ns);
  175. }
  176. return prefix;
  177. }
  178. private string GetQualifiedName (string name, string ns)
  179. {
  180. if (ns == String.Empty) return name;
  181. string prefix = GetNamespacePrefix (ns);
  182. if (prefix == String.Empty)
  183. return name;
  184. else
  185. return String.Format ("{0}:{1}", prefix, name);
  186. }
  187. protected abstract void InitCallbacks ();
  188. protected void TopLevelElement ()
  189. {
  190. topLevelElement = true;
  191. }
  192. protected void WriteAttribute (string localName, byte[] value)
  193. {
  194. WriteAttribute (localName, String.Empty, value);
  195. }
  196. protected void WriteAttribute (string localName, string value)
  197. {
  198. WriteAttribute (String.Empty, localName, String.Empty, value);
  199. }
  200. protected void WriteAttribute (string localName, string ns, byte[] value)
  201. {
  202. if (value == null)
  203. return;
  204. Writer.WriteStartAttribute (localName, ns);
  205. WriteValue (value);
  206. Writer.WriteEndAttribute ();
  207. }
  208. protected void WriteAttribute (string localName, string ns, string value)
  209. {
  210. WriteAttribute (null, localName, ns, value);
  211. }
  212. protected void WriteAttribute (string prefix, string localName, string ns, string value)
  213. {
  214. if (value == null)
  215. return;
  216. Writer.WriteAttributeString (prefix, localName, ns, value);
  217. }
  218. protected void WriteElementEncoded (XmlNode node, string name, string ns, bool isNullable, bool any)
  219. {
  220. if (name != string.Empty)
  221. {
  222. if (node == null)
  223. {
  224. if (isNullable)
  225. WriteNullTagEncoded (name, ns);
  226. }
  227. else
  228. {
  229. Writer.WriteStartElement (name, ns);
  230. node.WriteTo (Writer);
  231. Writer.WriteEndElement ();
  232. }
  233. }
  234. else
  235. node.WriteTo (Writer);
  236. }
  237. protected void WriteElementLiteral (XmlNode node, string name, string ns, bool isNullable, bool any)
  238. {
  239. if (name != string.Empty)
  240. {
  241. if (node == null)
  242. {
  243. if (isNullable)
  244. WriteNullTagLiteral (name, ns);
  245. }
  246. else
  247. {
  248. Writer.WriteStartElement (name, ns);
  249. node.WriteTo (Writer);
  250. Writer.WriteEndElement ();
  251. }
  252. }
  253. else
  254. node.WriteTo (Writer);
  255. }
  256. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value)
  257. {
  258. WriteElementQualifiedName (localName, String.Empty, value, null);
  259. }
  260. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value)
  261. {
  262. WriteElementQualifiedName (localName, ns, value, null);
  263. }
  264. protected void WriteElementQualifiedName (string localName, XmlQualifiedName value, XmlQualifiedName xsiType)
  265. {
  266. WriteElementQualifiedName (localName, String.Empty, value, xsiType);
  267. }
  268. protected void WriteElementQualifiedName (string localName, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  269. {
  270. localName = XmlCustomFormatter.FromXmlNCName (localName);
  271. WriteStartElement (localName, ns);
  272. if (xsiType != null) WriteXsiType (xsiType.Name, xsiType.Namespace);
  273. Writer.WriteString (FromXmlQualifiedName (value));
  274. WriteEndElement ();
  275. }
  276. protected void WriteElementString (string localName, string value)
  277. {
  278. WriteElementString (localName, String.Empty, value, null);
  279. }
  280. protected void WriteElementString (string localName, string ns, string value)
  281. {
  282. WriteElementString (localName, ns, value, null);
  283. }
  284. protected void WriteElementString (string localName, string value, XmlQualifiedName xsiType)
  285. {
  286. WriteElementString (localName, String.Empty, value, xsiType);
  287. }
  288. protected void WriteElementString (string localName, string ns, string value, XmlQualifiedName xsiType)
  289. {
  290. if (value == null) return;
  291. if (xsiType != null) {
  292. localName = XmlCustomFormatter.FromXmlNCName (localName);
  293. WriteStartElement (localName, ns);
  294. WriteXsiType (xsiType.Name, xsiType.Namespace);
  295. Writer.WriteString (value);
  296. WriteEndElement ();
  297. }
  298. else
  299. Writer.WriteElementString (localName, ns, value);
  300. }
  301. protected void WriteElementStringRaw (string localName, byte[] value)
  302. {
  303. WriteElementStringRaw (localName, String.Empty, value, null);
  304. }
  305. protected void WriteElementStringRaw (string localName, string value)
  306. {
  307. WriteElementStringRaw (localName, String.Empty, value, null);
  308. }
  309. protected void WriteElementStringRaw (string localName, byte[] value, XmlQualifiedName xsiType)
  310. {
  311. WriteElementStringRaw (localName, String.Empty, value, xsiType);
  312. }
  313. protected void WriteElementStringRaw (string localName, string ns, byte[] value)
  314. {
  315. WriteElementStringRaw (localName, ns, value, null);
  316. }
  317. protected void WriteElementStringRaw (string localName, string ns, string value)
  318. {
  319. WriteElementStringRaw (localName, ns, value, null);
  320. }
  321. protected void WriteElementStringRaw (string localName, string value, XmlQualifiedName xsiType)
  322. {
  323. WriteElementStringRaw (localName, String.Empty, value, null);
  324. }
  325. protected void WriteElementStringRaw (string localName, string ns, byte[] value, XmlQualifiedName xsiType)
  326. {
  327. if (value == null)
  328. return;
  329. WriteStartElement (localName, ns);
  330. if (xsiType != null)
  331. WriteXsiType (xsiType.Name, xsiType.Namespace);
  332. if (value.Length > 0)
  333. Writer.WriteBase64(value,0,value.Length);
  334. WriteEndElement ();
  335. }
  336. protected void WriteElementStringRaw (string localName, string ns, string value, XmlQualifiedName xsiType)
  337. {
  338. localName = XmlCustomFormatter.FromXmlNCName (localName);
  339. WriteStartElement (localName, ns);
  340. if (xsiType != null)
  341. WriteXsiType (xsiType.Name, xsiType.Namespace);
  342. Writer.WriteRaw (value);
  343. WriteEndElement ();
  344. }
  345. protected void WriteEmptyTag (string name)
  346. {
  347. WriteEmptyTag (name, String.Empty);
  348. }
  349. protected void WriteEmptyTag (string name, string ns)
  350. {
  351. name = XmlCustomFormatter.FromXmlName (name);
  352. WriteStartElement (name, ns);
  353. WriteEndElement ();
  354. }
  355. protected void WriteEndElement ()
  356. {
  357. WriteEndElement (null);
  358. }
  359. protected void WriteEndElement (object o)
  360. {
  361. if (o != null)
  362. serializedObjects.Remove (o);
  363. Writer.WriteEndElement ();
  364. }
  365. protected void WriteId (object o)
  366. {
  367. WriteAttribute ("id", GetId (o, true));
  368. }
  369. protected void WriteNamespaceDeclarations (XmlSerializerNamespaces ns)
  370. {
  371. if (ns == null)
  372. return;
  373. ICollection namespaces = ns.Namespaces.Values;
  374. foreach (XmlQualifiedName qn in namespaces) {
  375. if (Writer.LookupPrefix (qn.Namespace) == null)
  376. WriteAttribute ("xmlns", qn.Name, xmlNamespace, qn.Namespace);
  377. }
  378. }
  379. protected void WriteNullableQualifiedNameEncoded (string name, string ns, XmlQualifiedName value, XmlQualifiedName xsiType)
  380. {
  381. if (value != null)
  382. WriteElementQualifiedName (name, ns, value, xsiType);
  383. else
  384. WriteNullTagEncoded (name, ns);
  385. }
  386. protected void WriteNullableQualifiedNameLiteral (string name, string ns, XmlQualifiedName value)
  387. {
  388. if (value != null)
  389. WriteElementQualifiedName (name, ns, value);
  390. else
  391. WriteNullTagLiteral (name, ns);
  392. }
  393. protected void WriteNullableStringEncoded (string name, string ns, string value, XmlQualifiedName xsiType)
  394. {
  395. if (value != null)
  396. WriteElementString (name, ns, value, xsiType);
  397. else
  398. WriteNullTagEncoded (name, ns);
  399. }
  400. protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
  401. {
  402. if (value == null)
  403. WriteNullTagEncoded (name, ns);
  404. else
  405. WriteElementStringRaw (name, ns, value, xsiType);
  406. }
  407. protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
  408. {
  409. if (value == null)
  410. WriteNullTagEncoded (name, ns);
  411. else
  412. WriteElementStringRaw (name, ns, value, xsiType);
  413. }
  414. protected void WriteNullableStringLiteral (string name, string ns, string value)
  415. {
  416. if (value != null)
  417. WriteElementString (name, ns, value, null);
  418. else
  419. WriteNullTagLiteral (name, ns);
  420. }
  421. protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
  422. {
  423. if (value == null)
  424. WriteNullTagLiteral (name, ns);
  425. else
  426. WriteElementStringRaw (name, ns, value);
  427. }
  428. protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
  429. {
  430. if (value == null)
  431. WriteNullTagLiteral (name, ns);
  432. else
  433. WriteElementStringRaw (name, ns, value);
  434. }
  435. protected void WriteNullTagEncoded (string name)
  436. {
  437. WriteNullTagEncoded (name, String.Empty);
  438. }
  439. protected void WriteNullTagEncoded (string name, string ns)
  440. {
  441. Writer.WriteStartElement (name, ns);
  442. #if NET_1_1
  443. Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
  444. #else
  445. Writer.WriteAttributeString ("null", XmlSchema.InstanceNamespace, "1");
  446. #endif
  447. Writer.WriteEndElement ();
  448. }
  449. protected void WriteNullTagLiteral (string name)
  450. {
  451. WriteNullTagLiteral (name, String.Empty);
  452. }
  453. protected void WriteNullTagLiteral (string name, string ns)
  454. {
  455. WriteStartElement (name, ns);
  456. Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
  457. WriteEndElement ();
  458. }
  459. protected void WritePotentiallyReferencingElement (string n, string ns, object o)
  460. {
  461. WritePotentiallyReferencingElement (n, ns, o, null, false, false);
  462. }
  463. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
  464. {
  465. WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
  466. }
  467. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
  468. {
  469. WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
  470. }
  471. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
  472. {
  473. if (o == null)
  474. {
  475. if (isNullable) WriteNullTagEncoded (n, ns);
  476. return;
  477. }
  478. WriteStartElement (n, ns, true);
  479. CheckReferenceQueue ();
  480. if (callbacks.ContainsKey (o.GetType ()))
  481. {
  482. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  483. if (o.GetType ().IsEnum) {
  484. info.Callback (o);
  485. }
  486. else if (suppressReference) {
  487. Writer.WriteAttributeString ("id", GetId (o, false));
  488. if (ambientType != o.GetType ()) WriteXsiType(info.TypeName, info.TypeNs);
  489. info.Callback (o);
  490. }
  491. else {
  492. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  493. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  494. }
  495. }
  496. else
  497. {
  498. // Must be a primitive type
  499. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  500. if (td.SchemaType != SchemaTypes.Primitive)
  501. throw new InvalidOperationException ("Invalid type: " + o.GetType().FullName);
  502. WriteXsiType(td.XmlType, XmlSchema.Namespace);
  503. Writer.WriteString (XmlCustomFormatter.ToXmlString (td, o));
  504. }
  505. WriteEndElement ();
  506. }
  507. protected void WriteReferencedElements ()
  508. {
  509. if (referencedElements == null) return;
  510. if (callbacks == null) return;
  511. while (referencedElements.Count > 0)
  512. {
  513. object o = referencedElements.Dequeue ();
  514. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  515. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  516. WriteStartElement (info.TypeName, info.TypeNs, true);
  517. Writer.WriteAttributeString ("id", GetId (o, false));
  518. if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute
  519. WriteXsiType(info.TypeName, info.TypeNs);
  520. info.Callback (o);
  521. WriteEndElement ();
  522. }
  523. }
  524. protected void WriteReferencingElement (string n, string ns, object o)
  525. {
  526. WriteReferencingElement (n, ns, o, false);
  527. }
  528. protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
  529. {
  530. if (o == null)
  531. {
  532. if (isNullable) WriteNullTagEncoded (n, ns);
  533. return;
  534. }
  535. else
  536. {
  537. CheckReferenceQueue ();
  538. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  539. Writer.WriteStartElement (n, ns);
  540. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  541. Writer.WriteEndElement ();
  542. }
  543. }
  544. void CheckReferenceQueue ()
  545. {
  546. if (referencedElements == null)
  547. {
  548. referencedElements = new Queue ();
  549. InitCallbacks ();
  550. }
  551. }
  552. [MonoTODO]
  553. protected void WriteRpcResult (string name, string ns)
  554. {
  555. throw new NotImplementedException ();
  556. }
  557. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
  558. {
  559. if (serializable == null)
  560. {
  561. if (isNullable) WriteNullTagLiteral (name, ns);
  562. return;
  563. }
  564. else
  565. {
  566. Writer.WriteStartElement (name, ns);
  567. serializable.WriteXml (Writer);
  568. Writer.WriteEndElement ();
  569. }
  570. }
  571. protected void WriteStartDocument ()
  572. {
  573. if (Writer.WriteState == WriteState.Start)
  574. Writer.WriteStartDocument ();
  575. }
  576. protected void WriteStartElement (string name)
  577. {
  578. WriteStartElement (name, String.Empty, null, false);
  579. }
  580. protected void WriteStartElement (string name, string ns)
  581. {
  582. WriteStartElement (name, ns, null, false);
  583. }
  584. protected void WriteStartElement (string name, string ns, bool writePrefixed)
  585. {
  586. WriteStartElement (name, ns, null, writePrefixed);
  587. }
  588. protected void WriteStartElement (string name, string ns, object o)
  589. {
  590. WriteStartElement (name, ns, o, false);
  591. }
  592. protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
  593. {
  594. if (o != null)
  595. {
  596. if (serializedObjects.Contains (o))
  597. throw new InvalidOperationException ("A cirtular reference was detected while serializing an object of type " + o.GetType().Name);
  598. else
  599. serializedObjects [o] = o;
  600. }
  601. WriteState oldState = Writer.WriteState;
  602. string prefix = null;
  603. if (topLevelElement && ns != null && ns.Length != 0)
  604. {
  605. foreach (XmlQualifiedName qn in namespaces)
  606. if (qn.Namespace == ns) {
  607. prefix = qn.Name;
  608. writePrefixed = true;
  609. break;
  610. }
  611. }
  612. if (writePrefixed && ns != string.Empty)
  613. {
  614. name = XmlCustomFormatter.FromXmlName (name);
  615. if (prefix == null)
  616. prefix = Writer.LookupPrefix (ns);
  617. if (prefix == null || prefix.Length == 0)
  618. prefix = "q" + (++qnameCount);
  619. Writer.WriteStartElement (prefix, name, ns);
  620. } else
  621. Writer.WriteStartElement (name, ns);
  622. if (topLevelElement)
  623. {
  624. if (namespaces != null) {
  625. foreach (XmlQualifiedName qn in namespaces)
  626. {
  627. string currentPrefix = Writer.LookupPrefix (qn.Namespace);
  628. if (currentPrefix != null && currentPrefix.Length != 0) continue;
  629. WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
  630. }
  631. }
  632. topLevelElement = false;
  633. }
  634. }
  635. protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
  636. {
  637. string value;
  638. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  639. name = XmlCustomFormatter.FromXmlName (name);
  640. Writer.WriteStartElement (name, ns);
  641. if (o is XmlQualifiedName)
  642. value = FromXmlQualifiedName ((XmlQualifiedName) o);
  643. else
  644. value = XmlCustomFormatter.ToXmlString (td, o);
  645. if (xsiType)
  646. {
  647. if (td.SchemaType != SchemaTypes.Primitive)
  648. throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
  649. WriteXsiType (td.XmlType, XmlSchema.Namespace);
  650. }
  651. WriteValue (value);
  652. Writer.WriteEndElement ();
  653. }
  654. protected void WriteValue (byte[] value)
  655. {
  656. Writer.WriteBase64 (value, 0, value.Length);
  657. }
  658. protected void WriteValue (string value)
  659. {
  660. if (value != null)
  661. Writer.WriteString (value);
  662. }
  663. protected void WriteXmlAttribute (XmlNode node)
  664. {
  665. WriteXmlAttribute (node, null);
  666. }
  667. protected void WriteXmlAttribute (XmlNode node, object container)
  668. {
  669. XmlAttribute attr = node as XmlAttribute;
  670. if (attr == null)
  671. throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
  672. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace)
  673. {
  674. // The wsdl arrayType attribute needs special handling
  675. if (attr.LocalName == "arrayType") {
  676. string ns, type, dimensions;
  677. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  678. string value = GetQualifiedName (type + dimensions, ns);
  679. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, value);
  680. return;
  681. }
  682. }
  683. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
  684. }
  685. protected void WriteXsiType (string name, string ns)
  686. {
  687. if (ns != null && ns != string.Empty)
  688. WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
  689. else
  690. WriteAttribute ("type", XmlSchema.InstanceNamespace, name);
  691. }
  692. #endregion
  693. class WriteCallbackInfo
  694. {
  695. public Type Type;
  696. public string TypeName;
  697. public string TypeNs;
  698. public XmlSerializationWriteCallback Callback;
  699. }
  700. }
  701. }