问题描述
我有一个属性(_XMLPlaylist),我想在一个 XML 文件中序列化,如代码所示:
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();
}
}
Type XMLPlaylist 如下所示:
The Type XMLPlaylist looks as follows:
class XMLPlaylist
{
public XMLPlaylist()
{
Playlists = new List<Playlist>();
}
public List<Playlist> Playlists;
}
还有这样的播放列表:
class Playlist
{
public Playlist()
{
Casts = new List<Cast>();
}
public string Pl_Name;
public List<Cast> Casts;
}
'Cast' 拥有两个字符串.在 .NET 4 中,我在类名前使用了关键字 [Serializable],但不再有 [Serializable] 属性.
'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.
需要快速帮助!
'var ser = new XmlSerializer(typeof(XMLPlaylist));'处的错误:
Error at 'var ser = new XmlSerializer(typeof(XMLPlaylist));':
在 System.InvalidOperationException 中,发生类型为System.Xml.Serialization.ni.dll"的未处理错误.
In System.InvalidOperationException an unhandled error of type "System.Xml.Serialization.ni.dll" occurs.
推荐答案
XmlSerializer 只能序列化公共类 - 使您的类 XMLPlaylist 公开(还有您想要序列化的所有属性/类 - 因此 Playlist 也应该公开).
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).
这篇关于如何使用 Windows Phone 8 SDK 将类序列化为 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!