Guest User

Untitled

a guest
Feb 2nd, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.80 KB | None | 0 0
  1. [XmlRpcUrl("https://blabala.com/rpc")] // описание методов
  2. public interface Trac : IXmlRpcProxy
  3. {
  4. [XmlRpcMethod("ticket.get")] //
  5. object[] get(int id);
  6.  
  7. [XmlRpcMethod("ticket.getActions")] //
  8. object[] getActions(int id);
  9. }
  10.  
  11. public partial class DHL : Form
  12. {
  13. public DHL()
  14. {
  15. InitializeComponent();
  16. }
  17.  
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. int id = 6;
  21. get(id);
  22. }
  23.  
  24. private void get(int id)
  25. {
  26. Trac proxy;
  27. string user = "uzer";
  28. string password = "parol";
  29. proxy = XmlRpcProxyGen.Create<Trac>();
  30. proxy.Credentials = new System.Net.NetworkCredential(user, password);
  31. object[] arr = proxy.get(id);
  32. BinaryFormatter formatter = new BinaryFormatter();
  33. using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
  34. {
  35. formatter.Serialize(fs, arr);// тут и ошибка, что Тип CookComputing.XmlRpc.XmlRpcStruct в сборке CookComputing.XmlRpcV2, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a7d6e17aa302004d не отмечен как сериализуемый.
  36. }
  37. }
  38.  
  39. private void button4_Click(object sender, EventArgs e)
  40. {
  41. int id = 6;
  42. getActions( id);
  43. }
  44.  
  45. private void getActions(int id)
  46. {
  47. Trac proxy;
  48. string user = "uzer";
  49. string password = "parol";
  50. proxy = XmlRpcProxyGen.Create<Trac>();
  51. proxy.Credentials = new System.Net.NetworkCredential(user, password);
  52. object[] arr = proxy.getActions(id);
  53. BinaryFormatter formatter = new BinaryFormatter();
  54. using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
  55. {
  56. formatter.Serialize(fs, arr);//а тут нет такой ошибки
  57. }
  58. }
  59. }
  60.  
  61. public class Person
  62. {
  63. public string Name { get; set; }
  64. public int Age { get; set; }
  65. public DriversLicense License;
  66. }
  67.  
  68.  
  69. // An instance of this type will be part of the object graph and will need to be
  70. // serialized also.
  71. public class DriversLicense
  72. {
  73. public string Number { get; set; }
  74. }
  75.  
  76. public class PersonSurrogate : ISerializationSurrogate
  77. {
  78. /// <summary>
  79. /// Manually add objects to the <see cref="SerializationInfo"/> store.
  80. /// </summary>
  81. public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
  82. {
  83. Person person = (Person) obj;
  84. info.AddValue("Name", person.Name);
  85. info.AddValue("Age", person.Age);
  86. info.AddValue("License", person.License);
  87. }
  88.  
  89. /// <summary>
  90. /// Retrieves objects from the <see cref="SerializationInfo"/> store.
  91. /// </summary>
  92. /// <returns></returns>
  93. public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  94. {
  95. Person person = (Person)obj;
  96. person.Name = info.GetString("Name");
  97. person.Age = info.GetInt32("Age");
  98. person.License = (DriversLicense) info.GetValue("License", typeof(DriversLicense));
  99. return person;
  100. }
  101. }
  102.  
  103. public class DriversLicenseSurrogate : ISerializationSurrogate
  104. {
  105. /// <summary>
  106. /// Manually add objects to the <see cref="SerializationInfo"/> store.
  107. /// </summary>
  108. public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
  109. {
  110. DriversLicense license = (DriversLicense)obj;
  111. info.AddValue("Number", license.Number);
  112. }
  113.  
  114. /// <summary>
  115. /// Retrieves objects from the <see cref="SerializationInfo"/> store.
  116. /// </summary>
  117. /// <returns></returns>
  118. public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
  119. {
  120. DriversLicense license = (DriversLicense)obj;
  121. license.Number = info.GetString("Number");
  122. return license;
  123. }
  124. }
  125.  
  126. private static void SerializePerson(Person person)
  127. {
  128. if (person == null)
  129. throw new ArgumentNullException("person");
  130.  
  131. using (var memoryStream = new MemoryStream())
  132. {
  133. //Configure our surrogate selectors.
  134. var surrogateSelector = new SurrogateSelector();
  135. surrogateSelector.AddSurrogate(typeof (Person), new StreamingContext(StreamingContextStates.All),
  136. new PersonSurrogate());
  137. surrogateSelector.AddSurrogate(typeof (DriversLicense), new StreamingContext(StreamingContextStates.All),
  138. new DriversLicenseSurrogate());
  139.  
  140. //Serialize the object
  141. IFormatter formatter = new BinaryFormatter();
  142. formatter.SurrogateSelector = surrogateSelector;
  143. formatter.Serialize(memoryStream, person);
  144.  
  145. //Return to the beginning of the stream
  146. memoryStream.Seek(0, SeekOrigin.Begin);
  147.  
  148. //Deserialize the object
  149. Person deserializedPerson = (Person) formatter.Deserialize(memoryStream);
  150. }
  151. }
  152.  
  153. object[] arr = proxy.get(id); //момент получения данных
  154. DataTable dt = new DataTable();
  155. XmlRpcStruct arr3 = (XmlRpcStruct)arr[3];//проблема у меня была с выводом 3 его элемента объекта, поэтому его и преобразовываем
  156. XmlRpcStruct[] arr33 = new XmlRpcStruct[1];
  157. arr33[0] = arr3;
  158.  
  159.  
  160. dt = StructArrayToDT(arr33);//преобразуем в DataTable
  161.  
  162. //DA.Fill(dt);
  163. dataGridView1.DataSource = dt;//полученный DataTable можно вывести в DataGrid
  164.  
  165. textBox1.Text = textBox1.Text + arr[0] + "rn" + arr[1] + "rn" + arr[2] + "rn";// тут выводим 0-2 члены объекта, с которыми не было проблем
  166. BinaryFormatter formatter = new BinaryFormatter();//можно сериализовать полученный DataTable
  167. using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
  168. {
  169. formatter.Serialize(fs, dt);
  170. }
  171.  
  172.  
  173.  
  174.  
  175. public static DataTable StructArrayToDT(XmlRpcStruct[] data) //метод который из XmlRpcStruct[] делает DataTable
  176. {
  177. DataTable dt = new DataTable();
  178. if (data.Length == 0) { return dt; }
  179.  
  180. // do columns
  181. foreach (DictionaryEntry d in data[0])
  182. {
  183. dt.Columns.Add(d.Key.ToString(), typeof(object));
  184. }
  185.  
  186. foreach (XmlRpcStruct xmlstruct in data)
  187. {
  188. DataRow dr = dt.NewRow();
  189. foreach (DictionaryEntry d in xmlstruct)
  190. {
  191. try
  192. {
  193. dr[d.Key.ToString()] = d.Value;
  194. }
  195. catch (Exception ex)
  196. {
  197. // handle errors
  198. }
  199.  
  200. }
  201. dt.Rows.Add(dr);
  202. }
  203. return dt;
  204. }
Add Comment
Please, Sign In to add comment