/// <summary> /// Deserializes a given XElement into an entity, with the optional parent /// </summary> /// <param name="element">The xml element to derserialize into an entity</param> /// <param name="parent">The parent of the entity if any</param> /// <remarks>The method uses recursion to deserialize any children</remarks> /// <returns>The Deserialized version of the entity provided in the XElement</returns> private Entity DeserializeSceneEntity(XElement element, Entity parent = null) { Entity entity = new Entity(parent); entity.Name = element.Attribute(nameof(entity.Name))?.Value; string guidAttribute = element.Attribute(nameof(entity.GUID))?.Value; Guid guid; if (guidAttribute != null && Guid.TryParse(guidAttribute, out guid)) { entity.GUID = guid; } else { entity.GUID = Guid.NewGuid(); } IEnumerable <XElement> behaviours = element.Element("Components")?.Elements(); if (behaviours != null) { foreach (XElement behaviourElement in behaviours) { BehaviourComponent behaviour = BehaviourFactory.CreateWithName(behaviourElement.Name.LocalName, entity); if (behaviour != null) { behaviour.Deserialize(behaviourElement); entity.Components.Add(behaviour); } } } IEnumerable <XElement> children = element.Element("Children")?.Elements(); if (children != null) { foreach (XElement childElement in children) { Entity childEntity = DeserializeSceneEntity(childElement, entity); entity.Children.Add(childEntity); } } return(entity); }