Byte array and WriteXML/ReadXML without schema... RRS feed

  • Question

  • Hi.

    I have this annoying little problem that I have struggled with for a couple of days now. Here's the problem in a nut shell:

    1. I have a DataTable object which I write to a MemoryStream object using WriteXML. No schema.

    2. Then I use MemoryStream's ToArray method and the byte array is stored to another table which has a column and its datatype is System.Byte[].

    3. Then I write the second table's data set (there are also other table's in that data set) to an XML file using DataSet class' WriteXML methdod.

    4. Then I try to read the XML file to a DataSet object using ReadXML method. Because there's no schema every field's type is System.String. This is a problem only with the field whose original type is System.Byte[].

    The byte array is written in a strange string form and I have tried to read it using UnicodeEncoding, ASCIIEncoding, UTF8Encoding etc. I cannot change it back to the original form that DataTable would understand.

    When I read it to a string and convert it to a byte array using different encodings I still can't read it to a DataTable object using ReadXML methdod. If converted correctly the string should look like "<DataSet..." but it's always in the same form that it is in the XML file "PERhdGFT...".

    When I write the DataTable object back to an XML file using WriteXML and then read it back using ReadXML it works just fine. So the main question is: how do I read/convert the string that represents a byte array so that it can be read to a DataTable without crashing and writing it to a temp file and reading it back?

    I hope this wasn't explained too complicated (which it obviously is) and someone could help me out. :)



    Thursday, June 10, 2010 9:15 AM

All replies

  • I found this question earlier when I was searching for a similar answer. The 'secret' is that it's encoded as a Base64 string before it's written to XML. Here's what I used to convert from using WriteXML function back to my byte array. 

                    packedData = Convert.FromBase64String(dr["packedData"]);


    Monday, October 28, 2013 8:34 PM