Search code examples
c#clob

Decryption of clob data (in string form) to actual string in C#


My problem: I'm making a REST call to obtain some information and it returns an XML document, but the value I need is represented in CLOB form.

So what I have is a string representation of a CLOB e.g. "eJydVd1v4jgQf/dfkYd9uJOuEQFK6aE8OInTppeviw3... etc."

I want to see these details in plain text and I can't figure out how to do this. I've searched for decrypting/deserializing clobs but found nothing. I also found the System.Data.OracleClient namespace for C# but this only helps if you're reading a clob from a database, I believe.

So can anyone help me find a way to decrypt this clob data into string form or convert the string into the clob object oracle supplies?

Many Thanks,

Fintan


Solution

  • On my setup the xml clobs are encoded as Unicode.

    The using the following code can be used to get the string:

    using (ODC.OracleDataReader rdr = cmd.ExecuteReader())
    {
       if (rdr.Read())
       {
           var clob = rdr.GetOracleClob(0);
           // Can not simply use clob.Value - throws ORA-03113
           // Need to read input in chunks
           long length = clob.Length;
           List<byte> bytes = new List<byte>();
           int offset = 0;
           while (offset < length)
           {
              int readLength = (int)Math.Min(1024, length - offset);
              byte[] tmp = new byte[readLength];
              int readBytes = clob.Read(tmp, 0, readLength);
              offset += readLength;
              bytes.AddRange(tmp);
           }
        string xml = System.Text.Encoding.Unicode.GetString(bytes.ToArray()); 
        return DecodeXmlString(xml);
    }
    else
        return null;
    }