我的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Linq;
using System.Threading.Tasks;
namespace PlaylistEditor.Writers
{
public class XMLPlaylistWriter : IPlaylistWriter
{
public void Save(Playlist ply, string filePath)
{
StreamWriter writer = null;
try
{
XmlSerializer xs = new XmlSerializer(ply.GetType());
writer = new StreamWriter(filePath);
xs.Serialize(writer, ply);
}
finally
{
if (writer != null)
writer.Close();
writer = null;
}
}
public Playlist Load(string filePath)
{
return null;
}
}
}
我写道加载“return null;”我序列化并运行代码,但我不能做,并找到如何反序列化。谢谢你的帮助。
答案 0 :(得分:2)
您需要使用XmlReader
。
private Playlist Load(string filename)
{
Playlist playlist;
// Create an instance of the XmlSerializer specifying type and namespace.
var serializer = new
XmlSerializer(typeof(Playlist));
// A FileStream is needed to read the XML document.
using (var fs = new FileStream(filename, FileMode.Open))
{
using (var reader = XmlReader.Create(fs))
{
playlist = (Playlist) serializer.Deserialize(reader);
fs.Close();
}
}
return playlist;
}
来自msdn =>的参考资料link