XmlSerializationWriter.cs 21 KB

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