How to convert dataset to byte array in C#
By FoxLearn 8/29/2024 7:01:34 AM 110
How to convert dataset to byte array in C#
Convert the DataSet
to an XML format, then you can use the DataSet.WriteXml
method to write the XML to a MemoryStream
.
public byte[] ConvertDataSetToByteArray(DataSet dataSet) { if (dataSet == null) throw new ArgumentNullException(nameof(dataSet)); using (MemoryStream memoryStream = new MemoryStream()) { // Write the DataSet to XML dataSet.WriteXml(memoryStream, XmlWriteMode.IncludeSchema); // Convert MemoryStream to byte array return memoryStream.ToArray(); } }
To convert DataSet to Byte Array, you need to create a MemoryStream
. This stream will hold the XML representation of the DataSet
.
Next, Serialize DataSet to XML by using the WriteXml
method writes the DataSet
content to the MemoryStream
.
Finally, Get Byte Array by using the ToArray
method of MemoryStream
converts the stream's contents into a byte array.
How to convert byte array to dataset in C#
public DataSet ConvertByteArrayToDataSet(byte[] byteArray) { if (byteArray == null) throw new ArgumentNullException(nameof(byteArray)); using (MemoryStream memoryStream = new MemoryStream(byteArray)) { DataSet dataSet = new DataSet(); dataSet.ReadXml(memoryStream, XmlReadMode.InferSchema); return dataSet; } }
To convert Byte Array to DataSet, you need to create a MemoryStream
from Byte Array.
Next, Deserialize XML to DataSet by using the ReadXml
method reads the XML from the stream and populates the DataSet
.