Tuesday, February 21, 2006

C#: converting string into Stream

Most of the times when u deal with XML content and DataSets you might have a situation to load the XML content in string format in a DataSet and Dataset does not have any overloaded method of taking string as an parameter so this code will help you convert the string to stream which you can pass as a parameter to the DataSet.LoadXML(Stream s) method.

"string data = 'This is test data';
byte[] bytes = System.Text.Encoding.ASCII.GetBytes(data);
MemoryStream stream = new MemoryStream(bytes);"

Now "stream" contains the ascii bytes that make up the original string.