XmlSerializationWriter.cs 20 KB

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