我封装好的Socket通信是收发byte[]的,所以希望有个简单点的办法把DataSet转化成一个byte[]然后发送,接收后又要把这个byte[]转化回这个DataSet,只需要能够通过字段名访问转化回的这个DataSet里面的字段就行。请大家帮帮忙!

解决方案 »

  1.   

    尝试一下对象序列化
    [Serializable]
    我没有试过,但是直觉告诉我 应该可以,呵呵 
      

  2.   

    可以,以下转换的方法
    /// <summary>
    /// 把对象序列化并返回相应的字节
    /// </summary>
    /// <param name="pObj">需要序列化的对象</param>
    /// <returns>byte[]</returns>
    public  byte[] SerializeObject(object pObj)
    {
    if(pObj == null)
    return null;
    System.IO.MemoryStream _memory = new System.IO.MemoryStream();
    BinaryFormatter formatter = new BinaryFormatter();
    formatter.Serialize(_memory,pObj);
    _memory.Position = 0;
    byte[] read = new byte[_memory.Length];
    _memory.Read(read,0,read.Length);
    _memory.Close();
    return read;
    }
    /// <summary>
    /// 把字节反序列化成相应的对象
    /// </summary>
    /// <param name="pBytes">字节流</param>
    /// <returns>object</returns>
    public  object DeserializeObject(byte[] pBytes)
    {
    object _newOjb = null;
    if(pBytes == null)
    return _newOjb;
    System.IO.MemoryStream _memory = new System.IO.MemoryStream(pBytes);
    _memory.Position = 0;
    BinaryFormatter formatter = new BinaryFormatter();
    _newOjb = formatter.Deserialize(_memory);
    _memory.Close();
    return _newOjb;
    }
      

  3.   

    DataSet对象可以序列化,可以通过SOAP传递,同理可知肯定可转换成byte[]
      

  4.   

    提供一個方法,應該沒問題,把你的DataSet轉化為XML傳送,接受到以後再把你的XML轉換為DataSet,這樣就實現了你需要的功能了喔