Deserializing SQL varbinary in form of XML to C# byte[] -
i'm working on small proof of concept project, in client sends image file in byte[]
format , server inserts data database in image
column, varbinary(max)
. part works fine, however, i'm having issues trying convert data byte array , sending client.
in database, there stored procedure responsible getting data , returning in xml:
select ... asset.image 'image' assets asset asset.id = @id xml path('asset')
a sample query result looks (the actual value image
lot longer this, shortened make more readable):
<asset> ... <image>/9j/4aaqskzjrgabaqeayabgaad/2wb</image> </asset>
and in code, have generic method deserializes xml corresponding object:
internal static t executequery<t>(storedprocedures storedprocedure, parameterlist parameters) { using (var connection = getsqlconnection()) { using (var command = new sqlcommand(storedprocedure.tostring(), connection)) { command.commandtype = system.data.commandtype.storedprocedure; foreach (var parameter in parameters) { command.parameters.add(new sqlparameter(parameter.key.tostring(), parameter.value)); } connection.open(); var data = command.executescalar(); return deserializexml<t>(data.tostring()); } }
the asset
type (which i'm passing generic method has several properties, 1 of image
:
public class asset { public asset() { } ... [datamember] [xmlelement("image")] public byte[] image { get; set; } }
now when execute program, error:
there error in xml document
it seems deserializer having issues varbinary
value (it used work fine before added image
property). ideas why happening? need use memory stream convert data back?
you might want take @ how saving xml database. xml de-serialization fail if byte order mark being saved xml database (which case, depending on how you're doing it).
see this similar question on how eliminate byte order marks when saving xml data database.
Comments
Post a Comment