XmlRpcResponse.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. namespace Nwc.XmlRpc
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.IO;
  6. using System.Xml;
  7. /// <summary>Class designed to represent an XML-RPC response.</summary>
  8. public class XmlRpcResponse
  9. {
  10. private Object _value;
  11. /// <summary><c>bool</c> indicating if this response represents a fault.</summary>
  12. public bool IsFault;
  13. /// <summary>Basic constructor</summary>
  14. public XmlRpcResponse()
  15. {
  16. Value = null;
  17. IsFault = false;
  18. }
  19. /// <summary>Constructor for a fault.</summary>
  20. /// <param name="code"><c>int</c> the numeric faultCode value.</param>
  21. /// <param name="message"><c>String</c> the faultString value.</param>
  22. public XmlRpcResponse(int code, String message)
  23. : this()
  24. {
  25. SetFault(code, message);
  26. }
  27. /// <summary>The data value of the response, may be fault data.</summary>
  28. public Object Value
  29. {
  30. get { return _value; }
  31. set
  32. {
  33. IsFault = false;
  34. _value = value;
  35. }
  36. }
  37. /// <summary>The faultCode if this is a fault.</summary>
  38. public int FaultCode
  39. {
  40. get
  41. {
  42. if (!IsFault)
  43. return 0;
  44. else
  45. return (int)((Hashtable)_value)[XmlRpcXmlTokens.FAULT_CODE];
  46. }
  47. }
  48. /// <summary>The faultString if this is a fault.</summary>
  49. public String FaultString
  50. {
  51. get
  52. {
  53. if (!IsFault)
  54. return "";
  55. else
  56. return (String)((Hashtable)_value)[XmlRpcXmlTokens.FAULT_STRING];
  57. }
  58. }
  59. /// <summary>Set this response to be a fault.</summary>
  60. /// <param name="code"><c>int</c> the numeric faultCode value.</param>
  61. /// <param name="message"><c>String</c> the faultString value.</param>
  62. public void SetFault(int code, String message)
  63. {
  64. Hashtable fault = new Hashtable();
  65. fault.Add("faultCode", code);
  66. fault.Add("faultString", message);
  67. Value = fault;
  68. IsFault = true;
  69. }
  70. /// <summary>Form a useful string representation of the object, in this case the XML response.</summary>
  71. /// <returns><c>String</c> The XML serialized XML-RPC response.</returns>
  72. override public String ToString()
  73. {
  74. return XmlRpcResponseSerializer.Singleton.Serialize(this);
  75. }
  76. }
  77. }