日期:2014-05-19  浏览次数:20821 次

救急!!!!怎样将byte[]类型转换成Dataset类型的?
怎样将byte[]类型转换成Dataset类型的?各位大虾帮帮忙哈

------解决方案--------------------
1。DATASET 转化为 XML格式的STRING
private string DataSetToXml( DataSet xmlDS)
{
MemoryStream stream = null;
XmlTextWriter writer = null;

try
{
stream = new MemoryStream();
//从stream装载到XmlTextReader
writer = new XmlTextWriter(stream, Encoding.Unicode);

//用WriteXml方法写入文件.
xmlDS.WriteXml(writer);
int count = (int)stream.Length;
byte[] arr = new byte[count];
stream.Seek(0, SeekOrigin.Begin);
stream.Read(arr, 0, count);

UnicodeEncoding utf = new UnicodeEncoding();
return utf.GetString(arr).Trim();
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (writer != null) writer.Close();
}
}

2。STRING 转化为 byte[]
byte[] sendbyte = new byte[];
sendbyte = System.Text.Encoding.BigEndianUnicode.GetBytes(string.ToCharArray());

[网络传输]

3.byte[] 转化回STRING
string RecMessage = System.Text.Encoding.BigEndianUnicode.GetString(sendbyte);

4.XML格式的STRING 转化为 DATASET
public DataSet XmlToDataSet(string xmlData)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
DataSet xmlDS = new DataSet();
stream = new StringReader(xmlData);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null) reader.Close();
}

}