Files
win8phoneApp/CampusAppWP8/CampusAppWP8/Model/XmlModel.cs
2013-07-23 11:40:59 +02:00

88 lines
2.8 KiB
C#

//-----------------------------------------------------------------------------
// <copyright file="XmlModel.cs" company="BTU/IIT">
// Company copyright tag.
// </copyright>
// <author>fiedlchr</author>
// <sience>05.07.2013</sience>
//-----------------------------------------------------------------------------
namespace CampusAppWP8.Model
{
using System.Text;
using CampusAppWP8.Resources;
using CampusAppWP8.Utility;
/// <summary>
/// Xml model io handler class.
/// </summary>
/// <typeparam name="T">model type</typeparam>
public class XmlModel<T> : MainModel<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="XmlModel{T}" /> class.
/// </summary>
/// <param name="modelType">model io type</param>
/// <param name="fileName">filename of the data file</param>
/// <param name="url">url of the feed data</param>
public XmlModel(ModelType modelType, string fileName, string url)
: base(modelType, fileName, url)
{
this.ValidRootName = Constants.XMLRootElementName;
}
/// <summary>
/// Initializes a new instance of the <see cref="XmlModel{T}" /> class.
/// Use only if the model io type is file or feed, not both.
/// </summary>
/// <param name="modelType">model io type</param>
/// <param name="sourceName">name of the file or the url of the feed</param>
public XmlModel(ModelType modelType, string sourceName)
: base(modelType, sourceName)
{
}
/// <summary>
/// Gets or sets for the name of the root-tag
/// </summary>
protected string ValidRootName { get; set; }
/// <summary>
/// Create the model from a xml byte array.
/// </summary>
/// <param name="modelData">model data</param>
/// <returns>true, if succeeded</returns>
protected override bool DeserializeModel(byte[] modelData)
{
bool retValue = true;
string data = Encoding.UTF8.GetString(modelData, 0, modelData.Length);
T tempModel = XmlManager.DeserializationToModel<T>(data, this.ValidRootName);
if (tempModel != null)
{
this.Model = tempModel;
}
else
{
retValue = false;
}
return retValue;
}
/// <summary>
/// Serializes the model to a byte array.
/// </summary>
/// <returns>model data</returns>
protected override byte[] SerializeModel()
{
byte[] retValue = null;
if (this.Model != null)
{
retValue = Encoding.UTF8.GetBytes(XmlManager.SerializationToString<T>(this.Model));
}
return retValue;
}
}
}