Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [XmlRpcUrl("https://blabala.com/rpc")] // описание методов
- public interface Trac : IXmlRpcProxy
- {
- [XmlRpcMethod("ticket.get")] //
- object[] get(int id);
- [XmlRpcMethod("ticket.getActions")] //
- object[] getActions(int id);
- }
- public partial class DHL : Form
- {
- public DHL()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- int id = 6;
- get(id);
- }
- private void get(int id)
- {
- Trac proxy;
- string user = "uzer";
- string password = "parol";
- proxy = XmlRpcProxyGen.Create<Trac>();
- proxy.Credentials = new System.Net.NetworkCredential(user, password);
- object[] arr = proxy.get(id);
- BinaryFormatter formatter = new BinaryFormatter();
- using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
- {
- formatter.Serialize(fs, arr);// тут и ошибка, что Тип CookComputing.XmlRpc.XmlRpcStruct в сборке CookComputing.XmlRpcV2, Version=3.0.0.0, Culture=neutral, PublicKeyToken=a7d6e17aa302004d не отмечен как сериализуемый.
- }
- }
- private void button4_Click(object sender, EventArgs e)
- {
- int id = 6;
- getActions( id);
- }
- private void getActions(int id)
- {
- Trac proxy;
- string user = "uzer";
- string password = "parol";
- proxy = XmlRpcProxyGen.Create<Trac>();
- proxy.Credentials = new System.Net.NetworkCredential(user, password);
- object[] arr = proxy.getActions(id);
- BinaryFormatter formatter = new BinaryFormatter();
- using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
- {
- formatter.Serialize(fs, arr);//а тут нет такой ошибки
- }
- }
- }
- public class Person
- {
- public string Name { get; set; }
- public int Age { get; set; }
- public DriversLicense License;
- }
- // An instance of this type will be part of the object graph and will need to be
- // serialized also.
- public class DriversLicense
- {
- public string Number { get; set; }
- }
- public class PersonSurrogate : ISerializationSurrogate
- {
- /// <summary>
- /// Manually add objects to the <see cref="SerializationInfo"/> store.
- /// </summary>
- public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
- {
- Person person = (Person) obj;
- info.AddValue("Name", person.Name);
- info.AddValue("Age", person.Age);
- info.AddValue("License", person.License);
- }
- /// <summary>
- /// Retrieves objects from the <see cref="SerializationInfo"/> store.
- /// </summary>
- /// <returns></returns>
- public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
- {
- Person person = (Person)obj;
- person.Name = info.GetString("Name");
- person.Age = info.GetInt32("Age");
- person.License = (DriversLicense) info.GetValue("License", typeof(DriversLicense));
- return person;
- }
- }
- public class DriversLicenseSurrogate : ISerializationSurrogate
- {
- /// <summary>
- /// Manually add objects to the <see cref="SerializationInfo"/> store.
- /// </summary>
- public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
- {
- DriversLicense license = (DriversLicense)obj;
- info.AddValue("Number", license.Number);
- }
- /// <summary>
- /// Retrieves objects from the <see cref="SerializationInfo"/> store.
- /// </summary>
- /// <returns></returns>
- public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
- {
- DriversLicense license = (DriversLicense)obj;
- license.Number = info.GetString("Number");
- return license;
- }
- }
- private static void SerializePerson(Person person)
- {
- if (person == null)
- throw new ArgumentNullException("person");
- using (var memoryStream = new MemoryStream())
- {
- //Configure our surrogate selectors.
- var surrogateSelector = new SurrogateSelector();
- surrogateSelector.AddSurrogate(typeof (Person), new StreamingContext(StreamingContextStates.All),
- new PersonSurrogate());
- surrogateSelector.AddSurrogate(typeof (DriversLicense), new StreamingContext(StreamingContextStates.All),
- new DriversLicenseSurrogate());
- //Serialize the object
- IFormatter formatter = new BinaryFormatter();
- formatter.SurrogateSelector = surrogateSelector;
- formatter.Serialize(memoryStream, person);
- //Return to the beginning of the stream
- memoryStream.Seek(0, SeekOrigin.Begin);
- //Deserialize the object
- Person deserializedPerson = (Person) formatter.Deserialize(memoryStream);
- }
- }
- object[] arr = proxy.get(id); //момент получения данных
- DataTable dt = new DataTable();
- XmlRpcStruct arr3 = (XmlRpcStruct)arr[3];//проблема у меня была с выводом 3 его элемента объекта, поэтому его и преобразовываем
- XmlRpcStruct[] arr33 = new XmlRpcStruct[1];
- arr33[0] = arr3;
- dt = StructArrayToDT(arr33);//преобразуем в DataTable
- //DA.Fill(dt);
- dataGridView1.DataSource = dt;//полученный DataTable можно вывести в DataGrid
- textBox1.Text = textBox1.Text + arr[0] + "rn" + arr[1] + "rn" + arr[2] + "rn";// тут выводим 0-2 члены объекта, с которыми не было проблем
- BinaryFormatter formatter = new BinaryFormatter();//можно сериализовать полученный DataTable
- using (FileStream fs = new FileStream("arr.dat", FileMode.OpenOrCreate))
- {
- formatter.Serialize(fs, dt);
- }
- public static DataTable StructArrayToDT(XmlRpcStruct[] data) //метод который из XmlRpcStruct[] делает DataTable
- {
- DataTable dt = new DataTable();
- if (data.Length == 0) { return dt; }
- // do columns
- foreach (DictionaryEntry d in data[0])
- {
- dt.Columns.Add(d.Key.ToString(), typeof(object));
- }
- foreach (XmlRpcStruct xmlstruct in data)
- {
- DataRow dr = dt.NewRow();
- foreach (DictionaryEntry d in xmlstruct)
- {
- try
- {
- dr[d.Key.ToString()] = d.Value;
- }
- catch (Exception ex)
- {
- // handle errors
- }
- }
- dt.Rows.Add(dr);
- }
- return dt;
- }
Add Comment
Please, Sign In to add comment