开发者

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

开发者 https://www.devze.com 2022-12-19 02:12 出处:网络
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 p

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>
   开发者_StackOverflow /// 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:

  • http://msdn.microsoft.com/en-us/library/ms731923.aspx
  • http://geekswithblogs.net/frankw/archive/2008/08/29/serialization-issue-with-timestamp-in-linq-to-sql.aspx
  • http://msdn.microsoft.com/en-us/library/system.data.linq.binary.aspx


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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消