2013-08-06 97 views
0

我已經查看了所有關於序列化的東西,但不知道爲什麼這不起作用。我序列化myMediaInterface對象列表,這是視頻和歌曲類的基類。我把歌班放在下面。所有屬性爲null的屬性都被序列化爲xml文件,但所有具有值的屬性都不是。在序列化代碼被調用的時候,我在列表中有882首歌曲,882個序列化的歌曲出現在xml文件中,但沒有任何非null或0屬性。爲什麼我的屬性沒有被序列化,winrt c#xaml

[XmlInclude(typeof(Song))] 
[XmlInclude(typeof(Video))] 

public abstract class myMediaInterface 
{ 
    public string type { get; set; } 
    public string path { get; set; } 
    public String artist { get; set; } 
    public String title { get; set; } 
    public String album { get; set; } 
    public uint trackNumber { get; set; } 
    public String genre1 { get; set; } 
    public TimeSpan duration { get; set; } 
    public uint rating { get; set; } 
    public uint bitrate { get; set; } 
    public uint year { get; set; } 
    public List<String> genre { get; set; } 
    public int indexWithinParentCollection { get; set; } 
    public string displayName { get; set; } 
} 

public class Song : myMediaInterface 
{ 
    public String type 
    { 
     get 
     { 
      return "Song"; 
     } 
     set 
     { 
      value = "Song"; 
     } 
    } 
    public String path { get; set; } 
    public String artist { get; set; } 
    public String title { get; set; } 
    public String album { get; set; } 
    public uint trackNumber { get; set; } 
    public String genre1 { get; set; } 
    public TimeSpan duration { get; set; } 
    public uint rating { get; set; } 
    public uint bitrate { get; set; } 
    public uint year { get; set; } 
    public List<String> genre { get; set; } 
    public int indexWithinParentCollection { get; set; } 
    public String displayName { get; set; } 
} 

序列化代碼:

static async private Task saveState() 
    { 

     try 
     { 
      XmlSerializer xmlSer = new XmlSerializer(typeof(List<myMediaInterface>), new Type[]{typeof(Song)}); 
      StorageFile musicFile = await KnownFolders.MusicLibrary.CreateFileAsync("PlaylistXFastMusicLoading", CreationCollisionOption.ReplaceExisting); 
      IRandomAccessStream musicFileRandomAccess = await musicFile.OpenAsync(FileAccessMode.ReadWrite); 
      IOutputStream outputStream = musicFileRandomAccess.GetOutputStreamAt(0); 
      xmlSer.Serialize(outputStream.AsStreamForWrite(), _musicList); 
      outputStream.Dispose(); 
     } 
     catch (Exception exception) 
     { 
      System.Diagnostics.Debug.WriteLine(exception); 
     } 
    } 

async private void loadState() 
    { 
     try 
     { 
      XmlSerializer xmlSer = new XmlSerializer(typeof(List<myMediaInterface>), new Type[] { typeof(Song) }); 
      StorageFile sampleFile = await KnownFolders.MusicLibrary.CreateFileAsync("PlaylistXFastMusicLoading", CreationCollisionOption.OpenIfExists); 
      IInputStream fileStream = await sampleFile.OpenReadAsync(); 
      _musicList = (List<myMediaInterface>)xmlSer.Deserialize(fileStream.AsStreamForRead()); 
      fileStream.Dispose(); 
     } 
     catch (Exception exception) 
     { 
      System.Diagnostics.Debug.WriteLine(exception); 
     } 
    } 

和列表類的初始化

public static List<myMediaInterface> musicList = new List<myMediaInterface>(); 
    public static List<myMediaInterface> _musicList 
    { 
     get { return musicList; } 
     set { value = musicList; } 
    } 

回答

0

你重新實現遮擋在myMediaInterface所有繼承的成員。這意味着你在base和inherited class中有相同的字段。我想你想要這樣的:

[XmlInclude(typeof(Song))] 
public abstract class myMediaInterface 
{ 
    public abstract string type { get; set; } 
    public string path { get; set; } 
    public String artist { get; set; } 
    public String title { get; set; } 
    public String album { get; set; } 
    public uint trackNumber { get; set; } 
    public String genre1 { get; set; } 
    public TimeSpan duration { get; set; } 
    public uint rating { get; set; } 
    public uint bitrate { get; set; } 
    public uint year { get; set; } 
    public List<String> genre { get; set; } 
    public int indexWithinParentCollection { get; set; } 
    public string displayName { get; set; } 
} 

public class Song : myMediaInterface 
{ 
    public override String type 
    { 
     get 
     { 
      return "Song"; 
     } 
     set 
     { 
      value = "Song"; 
     } 
    } 
} 

說,我不認爲你需要有一個屬性「類型」,以確定你的對象的類型,你可以用你的object.GetType對象的類型()。

+0

謝謝!它現在有用,我猜我必須重新認識我的繼承。 – Zev

相關問題