How can I serialize a class to XML using Windows Phone 8 SDK?

穿精又带淫゛_ 提交于 2020-01-15 10:55:29

问题


I've got one attribute (_XMLPlaylist) that I want to serialize in a XML-file likeshown in the code:

private void btn_Save_Click(object sender, RoutedEventArgs e)
    {
        _Playlist.Pl_Name = tb_Name.Text.ToString();
        _XMLPlaylist.Playlists.Add(_Playlist);

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();

        using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(StudiCast.Resources.AppResources.Playlists, FileMode.CreateNew, isoStore))
        {
            using (StreamWriter writer = new StreamWriter(isoStream))
            {
                var ser = new XmlSerializer(typeof(XMLPlaylist));
                ser.Serialize(writer, _XMLPlaylist);
                writer.Close();
            }
            isoStream.Close();
        }
    }

The Type XMLPlaylist looks as follows:

class XMLPlaylist
{
    public XMLPlaylist()
    {
        Playlists = new List<Playlist>();
    }
    public List<Playlist> Playlists;
}

And the class Playlist like that:

 class Playlist
{
    public Playlist()
    {
        Casts = new List<Cast>();
    }

    public string Pl_Name;
    public List<Cast> Casts;
}

'Cast' owns two strings. In .NET 4 I used the keyword [Serializable] in front of the class's name but there isn't the [Serializable]-attribute anymore.

Need fast Help please!

Edit: Error at 'var ser = new XmlSerializer(typeof(XMLPlaylist));':

In System.InvalidOperationException an unhandled error of type "System.Xml.Serialization.ni.dll" occurs.


回答1:


XmlSerializer can serialize only public classes - make your class XMLPlaylist public (also all properties/classes you want to serialize - so Playlist should be public as well).



来源:https://stackoverflow.com/questions/20242642/how-can-i-serialize-a-class-to-xml-using-windows-phone-8-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!