Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.Serialization;
- using System.IO;
- namespace SerializeThingy
- {
- class Program
- {
- static void Main(string[] args)
- {
- List<string> myList = new List<string>();
- myList.Add("One");
- myList.Add("Two");
- myList.Add("Three");
- NetDataContractSerializer serializer = new NetDataContractSerializer();
- MemoryStream stream = new MemoryStream();
- serializer.Serialize(stream, myList);
- stream.Position = 0;
- Console.WriteLine(ASCIIEncoding.ASCII.GetString(stream.ToArray()));
- List<string> myList2 = (List<string>)serializer.Deserialize(stream);
- Console.WriteLine(myList2[0]);
- Console.ReadKey();
- }
- }
- }
- [Serializable]
- public struct Vector3
- {
- public double x, y, z;
- }
- class Program
- {
- static void Main(string[] args)
- {
- Vector3 vector = new Vector3();
- vector.x = 1;
- vector.y = 2;
- vector.z = 3;
- MemoryStream memoryStream = new MemoryStream();
- BinaryFormatter binaryFormatter = new BinaryFormatter();
- binaryFormatter.Serialize(memoryStream, vector);
- string str = System.Convert.ToBase64String(memoryStream.ToArray());
- //Store str into the database
- }
- }
- using System.Runtime.Serialization.Formatters.Binary;
- using System.IO;
- using System.Data.SqlClient;
- using System.Runtime.Serialization;
- public byte[] SerializeList<T>(List<T> list)
- {
- MemoryStream ms = new MemoryStream();
- BinaryFormatter bf = new BinaryFormatter();
- bf.Serialize(ms, list);
- ms.Position = 0;
- byte[] serializedList = new byte[ms.Length];
- ms.Read(serializedList, 0, (int)ms.Length);
- ms.Close();
- return serializedList;
- }
- public List<T> DeserializeList<T>(byte[] data)
- {
- try
- {
- MemoryStream ms = new MemoryStream();
- ms.Write(data, 0, data.Length);
- ms.Position = 0;
- BinaryFormatter bf = new BinaryFormatter();
- List<T> list = bf.Deserialize(ms) as List<T>;
- return list;
- }
- catch (SerializationException ex)
- {
- // Handle deserialization problems here.
- Debug.WriteLine(ex.ToString());
- return null;
- }
- }
- List<string> stringList = new List<string>() { "January", "February", "March" };
- byte[] data = SerializeList<string>(stringList);
- SqlParameter param = new SqlParameter("columnName", SqlDbType.Binary, data.Length);
- param.Value = data;
- etc...
Add Comment
Please, Sign In to add comment