I needed to convert a selection from a treeview into an XML string and then store into a database column of type LONG on Oracle and/or of type varchar(MAX) on MS SQL Server. The XML was not saved as a file first, so true it could have been saved in a string column, however, the database design dictated the decision making in this situation.
Therefore, I needed to take the string and convert it to a byte[]. This was done with the below code.
string aString = "The Best C# Programmer in the World-Ben Perkins";
ASCIIEncoding ascii = new ASCIIEncoding();
byte[] byteArray = ascii.GetBytes(aString);
When retrieved, I needed to convert, what could be either an Oracle LONG or MS SQL Server varchar(MAX) data type into a string. This was done with the below code.
Encoding encoder = new ASCIIEncoding();
string bString = encoder.GetString(byteArray);
Sometimes legacy system require programmers to make decisions and create solutions which are not perfect, but work well given the circumstances. This is an example of such a situation. Happy coding.
Download the source