using System.IO;
using System.Xml.Serialization;
using Microsoft.Samples.ServiceHosting.StorageClient;
namespace newtelligence.DasBlog.Runtime.Azure{
// Wrapper class to load and store clr objects from the blob storage,
// using the XmlSerializer.
internal class FileDataServiceAzure<T> : StreamDataServiceAzure
{
public FileDataServiceAzure()
: base()
{
//...
}
public T LoadFile(string filename, string category)
{
var stream = LoadFileStream(filename, category);
if (stream == null) { return default(T); }
using (stream)
{
var ser = new XmlSerializer(typeof(T));
stream.Position = 0;
return (T)ser.Deserialize(stream);
}
}
public void SaveFile(string filename, string category, T file)
{
using (var stream = new MemoryStream())
{
var ser = new XmlSerializer(typeof(T));
ser.Serialize(stream, file);
// put the straem back to the start
stream.Position = 0;
SaveFileStream(filename, category, stream);
}
}
}
}
|