Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Serialization and a Class with Binary Property - How can I get this to work?

I have the following serialization method shown below. The problem is that I am attempting to pass a class to it that contains a property of type Binary. The serialization is failing because of this property type. Is there any way I can serialize a class with a property of type Binary?

    private string Serialize<TEntity>(TEntity instance)
    {
        string retStr = "";
        XmlSerializer xs = new XmlSerializer(typeof(TEntity));
        System.IO.StringWriter writer = new System.IO.StringWriter();
        xs.Serialize(writer, instance);
        retStr = writer.ToString();
        writer.Close();

        return retStr;
    }

Here is the portion of the class that represents the Binary property.

    /// <summary>
    /// Row version number
    /// </summary>
    [DataMember(Order = 5)]
    public System.Data.Linq.Binary VersionNumber { get; set; }
like image 587
Randy Minder Avatar asked Dec 11 '25 01:12

Randy Minder


2 Answers

Bit late, but if someone else is looking for a solution, I will post mine here:

First, add System.Xml.Serialization.XmlIgnore to the Binary Property:

in my Case:

 <Global.System.Data.Linq.Mapping.ColumnAttribute(Storage:="_Bericht", DbType:="VarBinary(MAX)", UpdateCheck:=UpdateCheck.Never),
    System.Xml.Serialization.XmlIgnore> _
    Public Property Bericht() As System.Data.Linq.Binary
        Get
            Return Me._Bericht.Value
        End Get
        Set(value As System.Data.Linq.Binary)
            If (Object.Equals(Me._Bericht.Value, Value) = False) Then
                Me.OnBerichtChanging(Value)
                Me.SendPropertyChanging()
                Me._Bericht.Value = Value
                Me.SendPropertyChanged("Bericht")
                Me.OnBerichtChanged()
            End If
        End Set
    End Property

Second, add a new Property which will make the Binary value available as byte()

Public Property BerichtAsByte As Byte()
        Get
            If Me.Bericht Is Nothing Then Return Nothing
            Return Me.Bericht.ToArray
        End Get
        Set(value As Byte())
            If value Is Nothing Then
                Me.Bericht = Nothing
            Else
                Me.Bericht = New Data.Linq.Binary(value)
            End If
        End Set
    End Property

And that's it. Now the entity is serializable and stores all properties.

like image 123
awpross Avatar answered Dec 13 '25 15:12

awpross


Why not just convert the System.Linq.Binary to byte[]? Internally both are same, except System.Linq.Binary is immutable. Also Binary class doesn't have a default constructor hence the serialization fails.

Further reading:

  • http://msdn.microsoft.com/en-us/library/ms731923.aspx
  • Link
  • http://msdn.microsoft.com/en-us/library/system.data.linq.binary.aspx
like image 44
Vivek Avatar answered Dec 13 '25 15:12

Vivek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!