By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,466 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

C#-ADO.NET and XML: Reading a byte[ ] from a dataset in its XML representation

P: 1
Hello all. :)

My name is Oscar and Iím a novice on XML and dataset arguments.

I need some help to resolve (and understand :) ) the following problem:

I have stored some data in a SqlServer database. I read data stored in database using a DbDataAdapter which fill a dataset.
One of the columns DataType of the dataset is a System.Byte[ ] containing an array of bytes. (In the SqlServer database it is stored as VarBinary field).

For example: Byte[] _binaryCmd =0x15,0x00,0xFF,0xFF,0x12,0x5D,0x81,
0xD5,0x00,0xAB,0x00,0x00,0x00,0x01,0x00,0x00,0x00, 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};

I read the Xml representation of the data stored in the dataset, using the GetXml() methods, but Iím not able to read the value of the Byte[ ] field. These seems converted in some way. For example in the Xml element I read:


Whereas if I read the field of the dataset I can read correctly the byte array:

0x15,0x00,0xFF,0xFF,0x12,0x5D,0x81,0xD5,0x00,0xAB, 0x00,0x00,0x00,0x01,0x00,0x00,0x00,0xFF,0xFF,0xFF, 0xFF,0xFF,0xFF

The same situation happen if I put the dataset in an XmlDataDocument.

Please someone could explain me as I can obtain the original value of the Byte[ ] field in the XML representation?

Many thanks in advance. :)

This is an extract of my C# code

DataSet exportDataSet = new DataSet();
exportDataSet.DataSetName = "ExportedData";

/// Set the sql query to read data (_rCmd is a DBCommand object)
_rCmd.CommandText = String.Format(_getPagedMessagesTemplate, _msgTableName, pageIndex, pageSize);

/// Define adapter select command _wAdapter is a DbDataAdapter ///object.
_wAdapter.SelectCommand = _rCmd;

/// fill the dataset
_wAdapter.Fill(exportDataSet, _msgTableName);

/// Create an XmlDataDocument object related to our Dataset
XmlDataDocument xmlDataDoc = new XmlDataDocument(exportedData);

/// build an XmlTextWriter. destinationFile is a String that /// contains the path of the xml destination file.
XmlTextWriter xmlWriter = new XmlTextWriter(destinationFile,Encoding.Default);

/// Write data on the xml destination file
May 9 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.