Entwickler-Ecke

Datenbanken - C# Serialization.Formatters to Delphi


Delete - Mo 04.07.11 11:32
Titel: C# Serialization.Formatters to Delphi
Hallo,

ich hab mal eine Frage und steh auf dem Schlauch...

Mein Problem: Ich habe ein Binary-Feld (Blob) in einer MS SQL-Tabelle ind welches wahrscheinlich über C# per Serialization.Formatters geschrieben wurde, z.B. über folgende Methode (s. auch http://www.digitalcoding.com/Code-Snippets/C-Sharp/C-Code-Snippet-Save-object-to-file.html):

C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
public bool ObjectToFile(object _Object, string _FileName)
{
    try
    {
        // create new memory stream
        System.IO.MemoryStream _MemoryStream = new System.IO.MemoryStream();
        // create new BinaryFormatter
        System.Runtime.Serialization.Formatters.Binary.BinaryFormatter _BinaryFormatter
                    = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
        // Serializes an object, or graph of connected objects, to the given stream.
        _BinaryFormatter.Serialize(_MemoryStream, _Object);
        // convert stream to byte array
        byte[] _ByteArray = _MemoryStream.ToArray();
        // Open file for writing
        System.IO.FileStream _FileStream = new System.IO.FileStream(_FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
        // Writes a block of bytes to this stream using data from a byte array.
        _FileStream.Write(_ByteArray.ToArray(), 0, _ByteArray.Length);
        // close file stream
        _FileStream.Close();
        // cleanup
        _MemoryStream.Close();
        _MemoryStream.Dispose();
        _MemoryStream = null;
        _ByteArray = null;
        return true;
    }
    catch (Exception _Exception)
    {
        // Error
        Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
    }
 
    // Error occured, return null
    return false;
}

Das ist wahrscheinlich eine XML-Datei..., die als Binary in dem Feld gespeichert wurde.

Wie kann ich über Delphi diesen Blob/dieses Feld wieder auslesen, damit ich daraus wieder eine XML machen kann?

Hat einer vielleicht eine Idee?

Danke und viele Grüße
rd3


Moderiert von user profile iconNarses: Topic aus VCL (Visual Component Library) verschoben am Mo 04.07.2011 um 12:47


BenBE - Mo 04.07.11 16:07

Prinzipiell gibt es zwei Möglichkeiten:

1. Du schreibst Dir eine kleine DLL in C#, die Dir die Konvertierung erledigt und die Du von deinem Delphi-Programm aus aufrufst

oder

2. Du schaust Dir das Format an und deserialisierst Dir das selber und konvertierst es direkt im Delphi-Programm.

Für den zweiten Schritt kannst Du ggf. einen Blick auf den Code des Formatters in C# werfen als Ausgangspunkt.