XmlSerializationWriter.cs 21 KB

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