简体   繁体   中英

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; }

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.

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:

While not a total solution to your problem, please try the following code:

private string Serialize<TEntity>(TEntity instance)
{
    try
    {
        XmlSerializer xs = new XmlSerializer(typeof(TEntity));
        using (System.IO.StringWriter writer = new System.IO.StringWriter())
        {
            xs.Serialize(writer, instance);
            return writer.ToString();
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        throw;
    }
} 

Disclaimer: I am not an expert on WCF nor serialization, and the solution below is hacky at best and has only undergone brief verification.

public class HasBinaryProperty
{
    public byte[] versionBytes;

    public HasBinaryProperty()
    {//ignore this used it to test the serialization of the class briefly
        Random rand = new Random();
        byte[] bytes = new byte[20];
        rand.NextBytes(bytes);
        this.VersionNumber = new Binary(bytes);
    }

    [XmlIgnore]
    public Binary VersionNumber
    {
        get
        {
            return new Binary(this.versionBytes);
        }
        set
        {
            this.versionBytes = value.ToArray();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM