/// <summary> /// Returns the value of an xml-element if it exists or else returns the specified default value. /// </summary> /// <typeparam name="TValue">The type of the value to return.</typeparam> /// <param name="elementPath">The name of the attribute.</param> /// <param name="defaultValue">The fallback value if the attribute doesn't exists.</param> public static TValue ElementValue <TValue>(this XElement element, String elementPath, TValue defaultValue) { element.ThrowIfNull("element"); var childNames = elementPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); var childElement = element; for (var index = 0; index < childNames.Length && childElement != null; index++) { childElement = childElement.Element(childNames[index]); } if (childElement != null) { var typeConverter = default(TypeConverter); if (!TypeConverters.TryGetValue(typeof(TValue), out typeConverter)) { typeConverter = TypeDescriptor.GetConverter(typeof(TValue)); TypeConverters[typeof(TValue)] = typeConverter; } return((TValue)typeConverter.ConvertFromString(childElement.Value)); } return(defaultValue); }
public MessageColor Deserialize(XElement colorElement) { colorElement.ThrowIfNull("colorElement"); return new MessageColor( ColorSerializer.Instance.Deserialize((string)colorElement.Attribute("color"))); }
public Sprite Deserialize(XElement spriteElement) { spriteElement.ThrowIfNull("spriteElement"); return new Sprite( CoordinateSerializer.Instance.Deserialize((string)spriteElement.Attribute("coordinate")), CharacterSerializer.Instance.Deserialize(spriteElement.Element("character"))); }
public ActorInstanceLayer Deserialize(XElement actorInstanceLayerElement) { actorInstanceLayerElement.ThrowIfNull("actorInstanceLayerElement"); return new ActorInstanceLayer( (Guid)actorInstanceLayerElement.Attribute("boardId"), SizeSerializer.Instance.Deserialize((string)actorInstanceLayerElement.Attribute("size")), actorInstanceLayerElement.Elements("actorInstance").Select(ActorInstanceSerializer.Instance.Deserialize)); }
public Character Deserialize(XElement characterElement) { characterElement.ThrowIfNull("characterElement"); return new Character( Byte.Parse((string)characterElement.Attribute("symbol")), ColorSerializer.Instance.Deserialize((string)characterElement.Attribute("foregroundColor")), ColorSerializer.Instance.Deserialize((string)characterElement.Attribute("backgroundColor"))); }
public SpriteLayer Deserialize(XElement spriteLayerElement) { spriteLayerElement.ThrowIfNull("spriteLayerElement"); return new SpriteLayer( (Guid)spriteLayerElement.Attribute("boardId"), SizeSerializer.Instance.Deserialize((string)spriteLayerElement.Attribute("size")), spriteLayerElement.Elements("sprite").Select(SpriteSerializer.Instance.Deserialize)); }
public SoundEffect Deserialize(XElement soundEffectElement) { soundEffectElement.ThrowIfNull("soundEffectElement"); return new SoundEffect( (Guid)soundEffectElement.Attribute("id"), (string)soundEffectElement.Attribute("name"), (string)soundEffectElement.Attribute("description"), BinarySerializer.Instance.Deserialize((string)soundEffectElement.Element("data"))); }
public MessageAnswer Deserialize(XElement answerElement) { answerElement.ThrowIfNull("answerElement"); return new MessageAnswer( (Guid)answerElement.Attribute("id"), (string)answerElement.Attribute("text"), MessagePartSerializer.Instance.Deserialize(answerElement), EventHandlerCollectionSerializer.Instance.Deserialize(answerElement)); }
public Actor Deserialize(XElement actorElement) { actorElement.ThrowIfNull("actorElement"); return new Actor( (Guid)actorElement.Attribute("id"), (string)actorElement.Attribute("name"), (string)actorElement.Attribute("description"), CharacterSerializer.Instance.Deserialize(actorElement.Element("character"))); }
public Player Deserialize(XElement playerElement) { playerElement.ThrowIfNull("playerElement"); return new Player( (Guid)playerElement.Attribute("id"), (Guid)playerElement.Attribute("boardId"), CoordinateSerializer.Instance.Deserialize((string)playerElement.Attribute("coordinate")), CharacterSerializer.Instance.Deserialize(playerElement.Element("character")), EventHandlerCollectionSerializer.Instance.Deserialize(playerElement)); }
public Message Deserialize(XElement messageElement) { messageElement.ThrowIfNull("messageContainer"); return new Message( (Guid)messageElement.Attribute("id"), (string)messageElement.Attribute("name"), (string)messageElement.Attribute("description"), ColorSerializer.Instance.Deserialize((string)messageElement.Attribute("backgroundColor")), MessagePartSerializer.Instance.Deserialize(messageElement)); }
public MessageQuestion Deserialize(XElement questionElement) { questionElement.ThrowIfNull("questionElement"); return new MessageQuestion( (string)questionElement.Attribute("prompt"), ColorSerializer.Instance.Deserialize((string)questionElement.Attribute("questionForegroundColor")), ColorSerializer.Instance.Deserialize((string)questionElement.Attribute("unselectedAnswerForegroundColor")), ColorSerializer.Instance.Deserialize((string)questionElement.Attribute("selectedAnswerForegroundColor")), ColorSerializer.Instance.Deserialize((string)questionElement.Attribute("selectedAnswerBackgroundColor")), questionElement.Elements("answer").Select(MessageAnswerSerializer.Instance.Deserialize)); }
public Timer Deserialize(XElement timerElement) { timerElement.ThrowIfNull("timerElement"); return new Timer( (Guid)timerElement.Attribute("id"), (string)timerElement.Attribute("name"), (string)timerElement.Attribute("description"), TimeSpan.ParseExact((string)timerElement.Attribute("interval"), "c", CultureInfo.InvariantCulture), Enum<TimerState>.Parse((string)timerElement.Attribute("state")), TimeSpan.ParseExact((string)timerElement.Attribute("elapsed"), "c", CultureInfo.InvariantCulture), EventHandlerCollectionSerializer.Instance.Deserialize(timerElement)); }
public ActorInstance Deserialize(XElement actorInstanceElement) { actorInstanceElement.ThrowIfNull("actorInstanceElement"); return new ActorInstance( (Guid)actorInstanceElement.Attribute("id"), (string)actorInstanceElement.Attribute("name"), (string)actorInstanceElement.Attribute("description"), (Guid)actorInstanceElement.Attribute("actorId"), (Guid)actorInstanceElement.Attribute("boardId"), CoordinateSerializer.Instance.Deserialize((string)actorInstanceElement.Attribute("coordinate")), CharacterSerializer.Instance.Deserialize(actorInstanceElement.Element("character")), EventHandlerCollectionSerializer.Instance.Deserialize(actorInstanceElement)); }
public World Deserialize(XElement worldElement) { worldElement.ThrowIfNull("worldElement"); return new World( (Guid)worldElement.Attribute("id"), (int)worldElement.Attribute("version"), (string)worldElement.Attribute("title"), PlayerSerializer.Instance.Deserialize(worldElement.Element("startingPlayer")), worldElement.Elements("board").Select(BoardSerializer.Instance.Deserialize), worldElement.Elements("actor").Select(ActorSerializer.Instance.Deserialize), worldElement.Elements("message").Select(MessageSerializer.Instance.Deserialize), worldElement.Elements("timer").Select(TimerSerializer.Instance.Deserialize), worldElement.Elements("soundEffect").Select(SoundEffectSerializer.Instance.Deserialize), worldElement.Elements("song").Select(SongSerializer.Instance.Deserialize)); }
public Board Deserialize(XElement boardElement) { boardElement.ThrowIfNull("boardElement"); return new Board( (Guid)boardElement.Attribute("id"), (string)boardElement.Attribute("name"), (string)boardElement.Attribute("description"), SizeSerializer.Instance.Deserialize((string)boardElement.Attribute("size")), SpriteLayerSerializer.Instance.Deserialize(boardElement.Element("backgroundLayer")), SpriteLayerSerializer.Instance.Deserialize(boardElement.Element("foregroundLayer")), ActorInstanceLayerSerializer.Instance.Deserialize(boardElement.Element("actorInstanceLayer")), boardElement.Elements("boardExit").Select(BoardExitSerializer.Instance.Deserialize), boardElement.Elements("timer").Select(TimerSerializer.Instance.Deserialize), EventHandlerCollectionSerializer.Instance.Deserialize(boardElement)); }
/// <summary> /// Returns the value of an xml-attribute if it exists or else returns the specified default value. /// </summary> /// <typeparam name="TValue">The type of the value to return.</typeparam> /// <param name="attributeName">The name of the attribute.</param> /// <param name="defaultValue">The fallback value if the attribute doesn't exists.</param> public static TValue AttributeValue <TValue>(this XElement element, String attributeName, TValue defaultValue) { element.ThrowIfNull("element"); var attribute = element.Attribute(attributeName); if (attribute != null) { var typeConverter = default(TypeConverter); if (!TypeConverters.TryGetValue(typeof(TValue), out typeConverter)) { typeConverter = TypeDescriptor.GetConverter(typeof(TValue)); TypeConverters[typeof(TValue)] = typeConverter; } return((TValue)typeConverter.ConvertFromString(attribute.Value)); } return(defaultValue); }
public MessageText Deserialize(XElement textElement) { textElement.ThrowIfNull("textElement"); return new MessageText(textElement.Value); }
public MessageLineBreak Deserialize(XElement lineBreakElement) { lineBreakElement.ThrowIfNull("lineBreakElement"); return new MessageLineBreak(); }