XML序列化和具有二进制属性的类 - 如何使其工作?

时间:2010-02-05 19:31:08

标签: c# serialization xml-serialization

我有以下序列化方法。问题是我试图将类传递给包含Binary类型属性的类。由于此属性类型,序列化失败。有没有办法我可以使用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;
    }

这是表示二进制属性的类的一部分。

    /// <summary>
    /// Row version number
    /// </summary>
    [DataMember(Order = 5)]
    public System.Data.Linq.Binary VersionNumber { get; set; }

4 个答案:

答案 0 :(得分:3)

迟到了,但如果其他人正在寻找解决方案,我会在这里发帖:

首先,将System.Xml.Serialization.XmlIgnore添加到二进制属性:

在我的案例中:

 <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

其次,添加一个新属性,使二进制值可用作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

就是这样。现在该实体是可序列化的并存储所有属性。

答案 1 :(得分:2)

为什么不将System.Linq.Binary转换为byte []?内部两者都是相同的,除了System.Linq.Binary是不可变的。此外,Binary类没有默认构造函数,因此序列化失败。

进一步阅读:

答案 2 :(得分:0)

虽然不是问题的完整解决方案,但请尝试以下代码:

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

答案 3 :(得分:0)

免责声明:我不是WCF的专家,也不是序列化,下面的解决方案充其量只是简单的验证,并且只进行了简短的验证。

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