XmlSerializationWriter.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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. [MonoTODO ("Implement")]
  386. protected void WriteNullableStringEncodedRaw (string name, string ns, byte[] value, XmlQualifiedName xsiType)
  387. {
  388. throw new NotImplementedException ();
  389. }
  390. [MonoTODO ("Implement")]
  391. protected void WriteNullableStringEncodedRaw (string name, string ns, string value, XmlQualifiedName xsiType)
  392. {
  393. throw new NotImplementedException ();
  394. }
  395. protected void WriteNullableStringLiteral (string name, string ns, string value)
  396. {
  397. if (value != null)
  398. WriteElementString (name, ns, value, null);
  399. else
  400. WriteNullTagLiteral (name, ns);
  401. }
  402. [MonoTODO ("Implement")]
  403. protected void WriteNullableStringLiteralRaw (string name, string ns, byte[] value)
  404. {
  405. throw new NotImplementedException ();
  406. }
  407. [MonoTODO ("Implement")]
  408. protected void WriteNullableStringLiteralRaw (string name, string ns, string value)
  409. {
  410. throw new NotImplementedException ();
  411. }
  412. protected void WriteNullTagEncoded (string name)
  413. {
  414. WriteNullTagEncoded (name, String.Empty);
  415. }
  416. protected void WriteNullTagEncoded (string name, string ns)
  417. {
  418. Writer.WriteStartElement (name, ns);
  419. Writer.WriteAttributeString ("null", XmlSchema.InstanceNamespace, "1");
  420. Writer.WriteEndElement ();
  421. }
  422. protected void WriteNullTagLiteral (string name)
  423. {
  424. WriteNullTagLiteral (name, String.Empty);
  425. }
  426. protected void WriteNullTagLiteral (string name, string ns)
  427. {
  428. Writer.WriteStartElement (name, ns);
  429. Writer.WriteAttributeString ("nil", XmlSchema.InstanceNamespace, "true");
  430. Writer.WriteEndElement ();
  431. }
  432. protected void WritePotentiallyReferencingElement (string n, string ns, object o)
  433. {
  434. WritePotentiallyReferencingElement (n, ns, o, null, false, false);
  435. }
  436. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType)
  437. {
  438. WritePotentiallyReferencingElement (n, ns, o, ambientType, false, false);
  439. }
  440. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference)
  441. {
  442. WritePotentiallyReferencingElement (n, ns, o, ambientType, suppressReference, false);
  443. }
  444. protected void WritePotentiallyReferencingElement (string n, string ns, object o, Type ambientType, bool suppressReference, bool isNullable)
  445. {
  446. if (o == null)
  447. {
  448. if (isNullable) WriteNullTagEncoded (n, ns);
  449. return;
  450. }
  451. WriteStartElement (n, ns, true);
  452. CheckReferenceQueue ();
  453. if (callbacks.ContainsKey (o.GetType ()))
  454. {
  455. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  456. if (o.GetType ().IsEnum) {
  457. info.Callback (o);
  458. }
  459. else if (suppressReference) {
  460. Writer.WriteAttributeString ("id", GetId (o, false));
  461. if (ambientType != o.GetType ()) WriteXsiType(info.TypeName, info.TypeNs);
  462. info.Callback (o);
  463. }
  464. else {
  465. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  466. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  467. }
  468. }
  469. else
  470. {
  471. // Must be a primitive type
  472. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  473. if (td.SchemaType != SchemaTypes.Primitive)
  474. throw new InvalidOperationException ("Invalid type: " + o.GetType().FullName);
  475. WriteXsiType(td.XmlType, XmlSchema.Namespace);
  476. Writer.WriteString (XmlCustomFormatter.ToXmlString (td, o));
  477. }
  478. WriteEndElement ();
  479. }
  480. protected void WriteReferencedElements ()
  481. {
  482. if (referencedElements == null) return;
  483. if (callbacks == null) return;
  484. while (referencedElements.Count > 0)
  485. {
  486. object o = referencedElements.Dequeue ();
  487. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  488. WriteCallbackInfo info = (WriteCallbackInfo) callbacks[o.GetType()];
  489. WriteStartElement (info.TypeName, info.TypeNs, true);
  490. Writer.WriteAttributeString ("id", GetId (o, false));
  491. if (td.SchemaType != SchemaTypes.Array) // Array use its own "arrayType" attribute
  492. WriteXsiType(info.TypeName, info.TypeNs);
  493. info.Callback (o);
  494. WriteEndElement ();
  495. }
  496. }
  497. protected void WriteReferencingElement (string n, string ns, object o)
  498. {
  499. WriteReferencingElement (n, ns, o, false);
  500. }
  501. protected void WriteReferencingElement (string n, string ns, object o, bool isNullable)
  502. {
  503. if (o == null)
  504. {
  505. if (isNullable) WriteNullTagEncoded (n, ns);
  506. return;
  507. }
  508. else
  509. {
  510. CheckReferenceQueue ();
  511. if (!AlreadyQueued (o)) referencedElements.Enqueue (o);
  512. Writer.WriteStartElement (n, ns);
  513. Writer.WriteAttributeString ("href", "#" + GetId (o, true));
  514. Writer.WriteEndElement ();
  515. }
  516. }
  517. void CheckReferenceQueue ()
  518. {
  519. if (referencedElements == null)
  520. {
  521. referencedElements = new Queue ();
  522. InitCallbacks ();
  523. }
  524. }
  525. protected void WriteSerializable (IXmlSerializable serializable, string name, string ns, bool isNullable)
  526. {
  527. if (serializable == null)
  528. {
  529. if (isNullable) WriteNullTagLiteral (name, ns);
  530. return;
  531. }
  532. else
  533. {
  534. Writer.WriteStartElement (name, ns);
  535. serializable.WriteXml (Writer);
  536. Writer.WriteEndElement ();
  537. }
  538. }
  539. protected void WriteStartDocument ()
  540. {
  541. if (Writer.WriteState == WriteState.Start)
  542. Writer.WriteStartDocument ();
  543. }
  544. protected void WriteStartElement (string name)
  545. {
  546. WriteStartElement (name, String.Empty, null, false);
  547. }
  548. protected void WriteStartElement (string name, string ns)
  549. {
  550. WriteStartElement (name, ns, null, false);
  551. }
  552. protected void WriteStartElement (string name, string ns, bool writePrefixed)
  553. {
  554. WriteStartElement (name, ns, null, writePrefixed);
  555. }
  556. protected void WriteStartElement (string name, string ns, object o)
  557. {
  558. WriteStartElement (name, ns, o, false);
  559. }
  560. protected void WriteStartElement (string name, string ns, object o, bool writePrefixed)
  561. {
  562. if (o != null)
  563. {
  564. if (serializedObjects.Contains (o))
  565. throw new InvalidOperationException ("A cirtular reference was detected while serializing an object of type " + o.GetType().Name);
  566. else
  567. serializedObjects [o] = o;
  568. }
  569. WriteState oldState = Writer.WriteState;
  570. // Elements with schema namespace are always written prefixed
  571. if (ns == XmlSchema.Namespace) writePrefixed = true;
  572. string prefix = null;
  573. if (topLevelElement && ns != null && ns.Length != 0)
  574. {
  575. foreach (XmlQualifiedName qn in namespaces)
  576. if (qn.Namespace == ns) {
  577. prefix = qn.Name;
  578. writePrefixed = true;
  579. break;
  580. }
  581. }
  582. if (writePrefixed && ns != string.Empty)
  583. {
  584. name = XmlCustomFormatter.FromXmlName (name);
  585. if (prefix == null)
  586. prefix = Writer.LookupPrefix (ns);
  587. if (prefix == null || prefix.Length == 0) {
  588. if (ns == XmlSchema.Namespace) prefix = "xsd";
  589. else prefix = "q" + (++qnameCount);
  590. }
  591. Writer.WriteStartElement (prefix, name, ns);
  592. } else
  593. Writer.WriteStartElement (name, ns);
  594. if (topLevelElement)
  595. {
  596. if (namespaces != null) {
  597. foreach (XmlQualifiedName qn in namespaces)
  598. {
  599. string currentPrefix = Writer.LookupPrefix (qn.Namespace);
  600. if (qn.Namespace == XmlSchema.Namespace || qn.Namespace == XmlSchema.InstanceNamespace) {
  601. if (currentPrefix == qn.Name) continue;
  602. }
  603. else
  604. if (currentPrefix != null && currentPrefix.Length != 0) continue;
  605. WriteAttribute ("xmlns",qn.Name,xmlNamespace,qn.Namespace);
  606. }
  607. }
  608. topLevelElement = false;
  609. }
  610. }
  611. protected void WriteTypedPrimitive (string name, string ns, object o, bool xsiType)
  612. {
  613. string value;
  614. TypeData td = TypeTranslator.GetTypeData (o.GetType ());
  615. name = XmlCustomFormatter.FromXmlName (name);
  616. Writer.WriteStartElement (name, ns);
  617. if (o is XmlQualifiedName)
  618. value = FromXmlQualifiedName ((XmlQualifiedName) o);
  619. else
  620. value = XmlCustomFormatter.ToXmlString (td, o);
  621. if (xsiType)
  622. {
  623. if (td.SchemaType != SchemaTypes.Primitive)
  624. throw new InvalidOperationException (string.Format (unexpectedTypeError, o.GetType().FullName));
  625. WriteXsiType (td.XmlType, XmlSchema.Namespace);
  626. }
  627. WriteValue (value);
  628. Writer.WriteEndElement ();
  629. }
  630. protected void WriteValue (byte[] value)
  631. {
  632. Writer.WriteBase64 (value, 0, value.Length);
  633. }
  634. protected void WriteValue (string value)
  635. {
  636. if (value != null)
  637. Writer.WriteString (value);
  638. }
  639. protected void WriteXmlAttribute (XmlNode node)
  640. {
  641. WriteXmlAttribute (node, null);
  642. }
  643. protected void WriteXmlAttribute (XmlNode node, object container)
  644. {
  645. XmlAttribute attr = node as XmlAttribute;
  646. if (attr == null)
  647. throw new InvalidOperationException ("The node must be either type XmlAttribute or a derived type.");
  648. if (attr.NamespaceURI == XmlSerializer.WsdlNamespace)
  649. {
  650. // The wsdl arrayType attribute needs special handling
  651. if (attr.LocalName == "arrayType") {
  652. string ns, type, dimensions;
  653. TypeTranslator.ParseArrayType (attr.Value, out type, out ns, out dimensions);
  654. string value = GetQualifiedName (type + dimensions, ns);
  655. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, value);
  656. return;
  657. }
  658. }
  659. WriteAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, attr.Value);
  660. }
  661. protected void WriteXsiType (string name, string ns)
  662. {
  663. if (ns != null && ns != string.Empty)
  664. WriteAttribute ("type", XmlSchema.InstanceNamespace, GetQualifiedName (name, ns));
  665. else
  666. WriteAttribute ("type", XmlSchema.InstanceNamespace, name);
  667. }
  668. #endregion
  669. class WriteCallbackInfo
  670. {
  671. public Type Type;
  672. public string TypeName;
  673. public string TypeNs;
  674. public XmlSerializationWriteCallback Callback;
  675. }
  676. }
  677. }