问题
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