public static void PersonMessageToXML(PersonMessage obj) { XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage)); using (var fs = new FileStream("person.txt", FileMode.OpenOrCreate, FileAccess.Write)) { xmlSer.Serialize(fs, obj); } }
public static byte[] PersonMessageToXMLByteArray(PersonMessage obj) { XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage)); using (var ms = new MemoryStream()) { xmlSer.Serialize(ms, obj); return(ms.ToArray()); } }
public static PersonMessage XMLByteArrayToPersonMessage(byte[] arrBytes) { using (var memStream = new MemoryStream()) { XmlSerializer xmlSer = new XmlSerializer(typeof(PersonMessage)); memStream.Write(arrBytes, 0, arrBytes.Length); memStream.Seek(0, SeekOrigin.Begin); PersonMessage person = (PersonMessage)xmlSer.Deserialize(memStream); return(person); } }
static void Main(string[] args) { try { IPEndPoint ipPoint = new IPEndPoint(IPAddress.Parse(address), port); Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.Connect(ipPoint); Console.WriteLine("Input message: "); string message = Console.ReadLine(); string name = "Evgen"; byte[] foto = File.ReadAllBytes("pic.jpg"); PersonMessage p1 = new PersonMessage(1, name, message, new List <string> { "newMessage", "cSharp", "XML", "rulez" }, foto); Console.WriteLine("\nPackage:\n" + p1); byte[] persMess = PersonMessageToXMLByteArray(p1); PersonMessageToXML(p1); Console.WriteLine("\nDeserialized package:\n\n" + (XMLByteArrayToPersonMessage(persMess).ToString()) + "\n"); socket.Send(persMess); // GET ANSWER byte[] data = new byte[256]; StringBuilder builder = new StringBuilder(); int bytes = 0; do { bytes = socket.Receive(data, data.Length, 0); builder.Append(Encoding.Unicode.GetString(data, 0, bytes)); } while (socket.Available > 0); Console.WriteLine("Server says: " + builder.ToString()); // CLOSE SOCKET socket.Shutdown(SocketShutdown.Both); socket.Close(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.Read(); }