XmlSerializationWriter.cs 21 KB

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