finish xmlApi

This commit is contained in:
stubbfel
2013-06-17 11:50:01 +02:00
parent 52625e6bee
commit 25b05855a2
4 changed files with 156 additions and 2 deletions

View File

@@ -171,6 +171,7 @@
<DesignTime>True</DesignTime>
<DependentUpon>Constants.resx</DependentUpon>
</Compile>
<Compile Include="Utility\ApiEventHandler.cs" />
<Compile Include="Utility\Feed.cs" />
<Compile Include="Utility\FeedEventHandler.cs" />
<Compile Include="Utility\FileList.cs" />

View File

@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.Utility
{
/// <summary>
/// This class handle the events of a api <see cref="RestApi{T}"/>
/// </summary>
public class ApiEventHandler
{
/// <summary>
/// Initializes a new instance of the <see cref="ApiEventHandler" /> class.
/// </summary>
public ApiEventHandler()
{
}
#region Delegate&Events
/// <summary>
/// Delegate for the ready event
/// </summary>
public delegate void ApiReadyHandler();
/// <summary>
/// The ready event
/// </summary>
public event ApiReadyHandler ApiIsReadyEvent;
#endregion
#region Method
/// <summary>
/// Method fire a ready event
/// </summary>
public void FireApiReadyevent()
{
this.ApiIsReadyEvent();
}
#endregion
}
}

View File

@@ -1,12 +1,116 @@
using System;
using CampusAppWP8.Resources;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace CampusAppWP8.Utility
{
public class XmlApi
public class XmlApi<T> : RestApi
{
/// <summary>
/// The model of the feed
/// </summary>
private T model;
protected string validRootName = Constants.XMLRootElementName;
/// <summary>
/// EventHandler of the api
/// </summary>
private readonly ApiEventHandler eventHandler;
public XmlApi(Uri apiBaseAddress): base(apiBaseAddress)
{
this.eventHandler = new ApiEventHandler();
}
/// <summary>
/// Gets or sets for the model of the feed
/// </summary>
public T Model
{
get
{
return this.model;
}
set
{
if ((value == null && this.model != null) || !value.Equals(this.model))
{
this.model = value;
}
}
}
/// <summary>
/// Gets for the event-handler of the feed
/// </summary>
public ApiEventHandler EventHandler
{
get { return this.eventHandler; }
}
/// <summary>
/// Method will be execute if the download of the feed is completed and create the model
/// </summary>
/// <param name="sender">Sender of the event</param>
/// <param name="e">Arguments of the event</param>
public void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Exception downloadError = e.Error;
if (downloadError != null)
{
return;
}
string downloadResult = e.Result;
if (downloadResult != null && !downloadResult.Equals(string.Empty))
{
this.CreateModel(downloadResult);
}
}
/// <summary>
/// Method create the model of the feed
/// </summary>
/// <param name="xmlString">content of the feed</param>
private void CreateModel(string xmlString)
{
if (xmlString == null || xmlString == string.Empty)
{
return;
}
this.Deserialization(xmlString);
this.EventHandler.FireApiReadyevent();
}
/// <summary>
/// Method implement the deserialization a Xml-feed
/// </summary>
/// <param name="feedString">content of the feed</param>
protected override void Deserialization(string feedString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
XDocument document = XDocument.Parse(feedString);
if (!document.Root.Name.ToString().Equals(validRootName))
{
XElement content = document.Root;
document = new XDocument();
document.Add(new XElement(validRootName, content));
}
T model = (T)serializer.Deserialize(document.CreateReader());
if (model != null)
{
this.Model = model;
}
}
}
}

View File

@@ -18,6 +18,9 @@ namespace CampusAppWP8.Utility
/// <typeparam name="T">Type for model of the feed</typeparam>
public abstract class XmlFeed<T> : Feed<T>
{
/// <summary>
/// Variablo for the name of the roottag
/// </summary>
protected string validRootName = Constants.XMLRootElementName;
#region Constructor