XmlRpcSerializer.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Xml;
  7. /// <summary>Base class of classes serializing data to XML-RPC's XML format.</summary>
  8. /// <remarks>This class handles the basic type conversions like Integer to &lt;i4&gt;. </remarks>
  9. /// <seealso cref="XmlRpcXmlTokens"/>
  10. public class XmlRpcSerializer : XmlRpcXmlTokens
  11. {
  12. /// <summary>Serialize the <c>XmlRpcRequest</c> to the output stream.</summary>
  13. /// <param name="output">An <c>XmlTextWriter</c> stream to write data to.</param>
  14. /// <param name="obj">An <c>Object</c> to serialize.</param>
  15. /// <seealso cref="XmlRpcRequest"/>
  16. virtual public void Serialize(XmlTextWriter output, Object obj)
  17. {
  18. }
  19. /// <summary>Serialize the <c>XmlRpcRequest</c> to a String.</summary>
  20. /// <remarks>Note this may represent a real memory hog for a large request.</remarks>
  21. /// <param name="obj">An <c>Object</c> to serialize.</param>
  22. /// <returns><c>String</c> containing XML-RPC representation of the request.</returns>
  23. /// <seealso cref="XmlRpcRequest"/>
  24. public String Serialize(Object obj)
  25. {
  26. StringWriter strBuf = new StringWriter();
  27. XmlTextWriter xml = new XmlTextWriter(strBuf);
  28. xml.Formatting = Formatting.Indented;
  29. xml.Indentation = 4;
  30. Serialize(xml, obj);
  31. xml.Flush();
  32. String returns = strBuf.ToString();
  33. xml.Close();
  34. return returns;
  35. }
  36. /// <remarks>Serialize the object to the output stream.</remarks>
  37. /// <param name="output">An <c>XmlTextWriter</c> stream to write data to.</param>
  38. /// <param name="obj">An <c>Object</c> to serialize.</param>
  39. public void SerializeObject(XmlTextWriter output, Object obj)
  40. {
  41. if (obj == null)
  42. return;
  43. if (obj is byte[])
  44. {
  45. byte[] ba = (byte[])obj;
  46. output.WriteStartElement(BASE64);
  47. output.WriteBase64(ba, 0, ba.Length);
  48. output.WriteEndElement();
  49. }
  50. else if (obj is String)
  51. {
  52. output.WriteElementString(STRING, obj.ToString());
  53. }
  54. else if (obj is Int32)
  55. {
  56. output.WriteElementString(INT, obj.ToString());
  57. }
  58. else if (obj is DateTime)
  59. {
  60. output.WriteElementString(DATETIME, ((DateTime)obj).ToString(ISO_DATETIME));
  61. }
  62. else if (obj is Double)
  63. {
  64. output.WriteElementString(DOUBLE, obj.ToString());
  65. }
  66. else if (obj is Boolean)
  67. {
  68. output.WriteElementString(BOOLEAN, ((((Boolean)obj) == true) ? "1" : "0"));
  69. }
  70. else if (obj is IList)
  71. {
  72. output.WriteStartElement(ARRAY);
  73. output.WriteStartElement(DATA);
  74. if (((ArrayList)obj).Count > 0)
  75. {
  76. foreach (Object member in ((IList)obj))
  77. {
  78. output.WriteStartElement(VALUE);
  79. SerializeObject(output, member);
  80. output.WriteEndElement();
  81. }
  82. }
  83. output.WriteEndElement();
  84. output.WriteEndElement();
  85. }
  86. else if (obj is IDictionary)
  87. {
  88. IDictionary h = (IDictionary)obj;
  89. output.WriteStartElement(STRUCT);
  90. foreach (String key in h.Keys)
  91. {
  92. output.WriteStartElement(MEMBER);
  93. output.WriteElementString(NAME, key);
  94. output.WriteStartElement(VALUE);
  95. SerializeObject(output, h[key]);
  96. output.WriteEndElement();
  97. output.WriteEndElement();
  98. }
  99. output.WriteEndElement();
  100. }
  101. }
  102. }
  103. }