Good day!

There is a MySQL database located on the server. In one of the database tables, data is presented in blob format (binary xml representation). I need to isolate this data and present it to the user, displaying it in richtextbox.

Immediately I say that I never had to work with xml, but this task should be solved right now.

There was a naive variant to derive everything as usual, but it turned out to be useless:

static string connectionString = "тут путь до сервера"; static MySqlConnection connection = new MySqlConnection(connectionString); string query = "SELECT ReqData, AnsData FROM tab_482 WHERE Npp = 545" ; connection.Open(); MySqlCommand command = new MySqlCommand(query, connection); MySqlDataReader reader = command.ExecuteReader(); richTextBox1.Text = reader["ReqData"].ToString(); richTextBox2.Text = reader["AnsData"].ToString(); reader.Close(); connection.Close(); 

After visiting several sites, it became clear that you need to use a special reader for xml, but in those examples you refer directly to the file, and how to specify the path to the table cell via the SQl query ...

There was another option:

 byte[] barray = Encoding.Unicode.GetBytes(((reader["ReqData"]).ToString()).ToCharArray()); string str = Encoding.Unicode.GetString(barray); richTextBox1.Text = str; 

but in both cases only System.byte[] is displayed.

Tell me, how can I get the data from the database so that the user sees the xml code?

  • In the second version, ToString() and ToCharArray() are not needed - Andrey NOP
  • Try something like System.Text.Encoding.Default.GetString(reader["ReqData"]) - Andrew NOP
  • To get the bytes from the reader, try it bytes to the bytes var reqBytes = (byte[] reader["ReqData"];) - tym32167
  • @AndreyNOP reader["ReqData"] has object type, so the compiler swears that the object in byte[] cannot be overtaken - Violofan

1 answer 1

Thank you all for your help. As a result, I got the following:

 richTextBox1.Text = Encoding.Default.GetString((byte[])reader["ReqData"]); 

In this case, all data is displayed in one line.

To interpret into a more readable version (provided that a valid xml is stored in the database), you should load it into an XDocument and use the ToString() method:

 var xdoc = XDocument.Parse(Encoding.Default.GetString((byte[])reader["ReqData"])); richTextBox1.Text = xdoc.ToString();