2013-03-01 30 views
1

我有這3類:繼承XmlSerializer的C#

Class Image : Asset 
Class Sound : Asset 
Class Video : Asset 

一切連載好的,但是當我創建這個項目:

Class Master 
List<Asset> assets //property 

例如這個類的一個實例:

Image i = new Image(); 
Sound s = new Sound(); 
Video v = new Video(); 
Master m = new Master(new List<Asset>{i,s,v}) 

它不會例外地序列化 「InvalidOperationException - 生成XML文檔「 和innerException:{」類型MyApplication.Video不是預期的。使用XmlInclude或SoapInclude屬性來指定不是靜態已知類型的「}

..任何想法?

+1

你可以用幫助例外信息 – Freeman 2013-03-01 14:21:35

+0

您是否已將屬性添加到您的類中?Image類是否包含任何filereader?是否使用NonSerialized屬性標記了所有非序列化的成員? – Tearsdontfalls 2013-03-01 14:23:20

+0

Image,Sound,Video實例中的每一個序列化正常沒有Seri​​alizeable attri bute沒有圖像不包含filereader .. – sstauross 2013-03-01 14:26:14

回答

5

添加XmlInclude屬性上Asset類:

[XmlInclude(typeof(Video))] 
[XmlInclude(typeof(Sound))] 
[XmlInclude(typeof(Image))] 
public class Asset 
{ 
    ... 
} 
+0

完美!非常感謝! ;) – sstauross 2013-03-01 14:31:08