0

I have following problem. I am coding in C# and I need retrieve XML from BLOB and then work with it (with XML).

Part from my code

...
var xmldoc = new XmlDocument();
OracleCommand oraCmd = new OracleCommand();
oraCmd.Connection = db.oraConnection;
oraCmd.CommandText = "SELECT XML_IN_BLOB FROM TABLE_X"; 
oraCmd.CommandType = CommandType.Text;
OracleDataReader dr = oraCmd.ExecuteReader();
dr.Read();
while (dr.Read())
{
// 1. convert dr. value into XML //var xmldoc
// 2. update XML element
// 3. store/update BLOB in TABLE_X
}
db.Close();

Do you have any ideas?

Anton Lieskovsky
  • 179
  • 2
  • 3
  • 14
  • If you can, change `BLOB` to `CLOB`, and work as a .Net `String`, as a common datatype `String xml_str = reader.GetValue(1)`, or if you use `OracleDataReader` as I see, you can call `reader.GetOracleLob`, goal is to get string in easy way, and after that use `XmlDocument.LoadXml(String)`. – Seyran Apr 26 '17 at 12:38

2 Answers2

0
  1. Examine the documentation of OracleDataReader to determine how to get a String Field out of it.
  2. Use XmlDocument.LoadXml(String) to create an editable structure from the string
  3. Examine the documentation of XmlDocument to determine how to change an element
  4. Saving the xml back into a String is a little more complicated but you can follow this example.
Community
  • 1
  • 1
Kempeth
  • 1,856
  • 2
  • 22
  • 37
0

If you are going to update the rows then you need to change your SELECT to include an Id. You will also need structure to keep all the ids and modified xml bytes.

var xmlsToUpdate = new Dictionary<int, byte[]>();
while (dr.Read())
{

    //Open it as a blob, then load the xml from the Stream.
    var id     = dr.GetInt32(0);
    var stream = dr.GetOracleLob(1);
    var xml    = new XmlDocument();
    xml.Load(stream);

    //manipulate xml

    var ms = new MemoryStream();
    xml.Save(ms);
    var bytes = ms.ToArray();
    xmlsToUpdate.Add(id, bytes);
}

After thar loop over the list and execute an update statement:

cmd.CommandText = "UPDATE TABLEX SET XML_IN_BLOB = :data WHERE id=" + item.Key;
cmd.Parameters.Add(":data", OracleType.Blob);
cmd.Parameters[":data"].Value = item.Value;

And don't forget to dispose the disposables. It might be easier to do with an ORM.

AlexDev
  • 4,049
  • 31
  • 36