Reputation: 17
My code:
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;
}
}
}
I wrote to load "return null;" i serialized that and ran code, but i can't do and find how to deserialize. thanks for your help.
Upvotes: 0
Views: 56
Reputation: 6766
You need to use 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;
}
reference from msdn => link
Upvotes: 2