通过套接字对对象进行XML序列化

时间:2013-04-18 22:29:14

标签: c# .net tcp xml-serialization xml-deserialization

我的想法 - >客户端服务器系统,通过TCP套接字交换文本消息(字符串)。我希望客户端和服务器之间的协议基于XML。因为套接字之间的信息是byte发送的,所以我必须进行转换。所以这就是我的所作所为: 属性类型为TheMessage的类string。我使用要作为对象属性发送的字符串来创建该类的对象,并将其从Object发送到byte[]XmlSerialization。另一方面,我做了反过来的过程。 这是我从客户端序列化并发送到服务器的方式:

msg.Message = Console.ReadLine();
byte[] writeBuff = XmlRefacrotClient.ObjectToByteArray(msg);
Stream stm = client.GetStream();
stm.Write(writeBuff, 0, writeBuff.Length);

这是我用于序列化的方法:

public static byte[] ObjectToByteArray(TheMessage obj)
{
    try
    {
        MemoryStream ms = new MemoryStream();
        XmlSerializer xmlS = new XmlSerializer(typeof(Message.TheMessage));
        XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8);

        xmlS.Serialize(xmlTW, obj);
        ms = (MemoryStream)xmlTW.BaseStream;

        return ms.ToArray();
    }
    catch(Exception)
    {
        throw;
    }
}

这就是我在服务器端接收数据的方式:

byte[] readBuff = new byte[1024];
s.Receive(readBuff);
String str = (XmlRefactorServer.ByteArrayToObject(readBuff)).ToString();

Console.WriteLine(str);

这是反序列化的方法:

public static Object ByteArrayToObject(byte[] arr)
{
    try
    {
        XmlSerializer xmlS = new XmlSerializer(typeof(Message.TheMessage));
        MemoryStream ms = new MemoryStream();
        XmlTextWriter xmlTW = new XmlTextWriter(ms, Encoding.UTF8);

        return xmlS.Deserialize(ms);
    }
    catch(Exception)
    {
        throw;
    }
}

return ByteArrayToObject方法的InvalidOperationException之前,一切都会顺利进行。我在There is an error in XML document (0, 0).行上获得return xmlS.Deserialize(ms);个描述{{1}}。

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

试试这个。它更清洁:

  //---------------------------------------------------------------------
        public static Byte[] ObjectToByteArray<T>(T obj)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer xmlS = new XmlSerializer(typeof(T));
                xmlS.Serialize(ms, obj);

                return ms.ToArray();
            }
        }
        //---------------------------------------------------------------------
        public static T ByteArrayObject<T>(Byte[] bObj)
        {
            using (MemoryStream ms = new MemoryStream(bObj))
            {
                XmlSerializer xmlS = new XmlSerializer(typeof(TheMessage));
                return (T)xmlS.Deserialize(ms);
            }
        }
        //---------------------------------------------------------------------
        public static void Sending(Byte[] bData)
        {
            Stream stm = client.GetStream();

            // always write size first when using TCP
            Byte[] bSize = BitConverter.GetBytes(bData.Length);
            stm.Write(bSize, 0, bSize.Length);
            stm.Write(bData, 0, bData.Length);
        }

        //---------------------------------------------------------------------
        public static void Receiving(Byte[] bData)
        {
            Byte[] bSize = new Byte[sizeof(Int32)];
            s.Read(bSize, 0, bSize.Length);

            Byte[] bData = new Byte[BitConverter.ToInt32(bSize, 0)];
            s.Read(bData, 0, bData.Length);

            TheMessage m = ByteArrayObject<TheMessage>(bData);
        }