使用DataContractSerializer进行序列化

时间:2014-07-03 22:08:19

标签: c# xml windows-phone-8 serialization datacontractserializer

我想要使用DataContract Serializer在有效的xml中序列化的 IProductDescription列表。这样我以后可以反序化它。这是实现ProductDescription接口的IProductDescription类。

[DataContract]
[KnownType(typeof(ProductDescription))]
public class ProductDescription : IProductDescription
{
    [DataMember]
    public List<AssetDescription> Assets { get; set; }

    [DataMember]
    public string ProductId { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]  
    public string FormattedPrice { get; set; }

    [DataMember]
    public DateTime? ExpirationDate { get; set; }

    [DataMember]
    public ProductStatus ProductStatus { get; set; }

    [DataMember]
    public string ProductPageUrl { get; set; }

    [DataMember]
    public string PackageDownloadLocationUrl { get; set; }

}

这是我的AssetDescription类

[DataContract]
public class AssetDescription
{
    [DataMember]
    public string Type { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public string IconUrl { get; set; }

}

现在我想序列化它。我已经有了序列化的实现。在这里和那里阅读后,我知道我必须添加一些knowntype属性,但我没有成功。所以有人可以帮我解决这个问题。

编辑: - 在添加属性之后,事情会被序列化,但错误的方式就像我的序列化字符串的顶部一样。

<ArrayOfanyType xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <anyType xmlns:d2p1="http://schemas.datacontract.org/2004/07/ProductDescriptionModel" i:type="d2p1:ProductDescription">
    <d2p1:Assets>

但是当我对它进行deserilize时,它会抛出一个异常,即ArrayType不应该是 ArrayOfanyType

我想序列化/去序列化IProductDescription列表

1 个答案:

答案 0 :(得分:3)

尝试将KnownType放入序列化程序中。

void Main()
{
    var serializer = new DataContractSerializer(typeof(IProductDescription),  knownTypes: new[] { typeof(ProductDescription) });

    using(var writer = new XmlTextWriter("C:\\Temp\\test.xml", Encoding.UTF8))
    {
        serializer.WriteObject(writer, new ProductDescription());   
    }

    using(var reader = new XmlTextReader("C:\\Temp\\test.xml"))
    {
        var o = serializer.ReadObject(reader) as IProductDescription;

        Console.WriteLine(o.ToString());
    }
}

public enum ProductStatus { One, Two, Three }

public interface IProductDescription
{
    [DataMember]
    List<AssetDescription> Assets { get; set; }

    [DataMember]
    string ProductId { get; set; }

    [DataMember]
    string Description { get; set; }

    [DataMember]  
    string FormattedPrice { get; set; }

    [DataMember]
    DateTime? ExpirationDate { get; set; }

    [DataMember]
    ProductStatus ProductStatus { get; set; }

    [DataMember]
    string ProductPageUrl { get; set; }

    [DataMember]
    string PackageDownloadLocationUrl { get; set; }

}

[DataContract]
public class ProductDescription : IProductDescription
{
    [DataMember]
    public List<AssetDescription> Assets { get; set; }

    [DataMember]
    public string ProductId { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]  
    public string FormattedPrice { get; set; }

    [DataMember]
    public DateTime? ExpirationDate { get; set; }

    [DataMember]
    public ProductStatus ProductStatus { get; set; }

    [DataMember]
    public string ProductPageUrl { get; set; }

    [DataMember]
    public string PackageDownloadLocationUrl { get; set; }

}

[DataContract]
public class AssetDescription
{
    [DataMember]
    public string Type { get; set; }

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public string Description { get; set; }

    [DataMember]
    public string IconUrl { get; set; }

}