/// <summary> /// Create XML Node from configuration entity /// </summary> /// <param name="entity">Entity to process</param> /// <returns>Converted XML node</returns> private XmlNode TransformEntityToNode(BaseEntity entity) { if (entity == null) { throw new ArgumentNullException("entity"); } XmlNode resultNode; var xmlSerializer = new XmlSerializer(entity.GetType()); var namespaces = new XmlSerializerNamespaces(); namespaces.Add(string.Empty, string.Empty); using (var memoryStream = new MemoryStream()) { try { xmlSerializer.Serialize(memoryStream, entity, namespaces); } catch (InvalidOperationException) { return null; } memoryStream.Position = 0; var serializationDoc = new XmlDocument(); serializationDoc.Load(memoryStream); resultNode = serializationDoc.DocumentElement; } return outputDocument.ImportNode(resultNode, true); }
/// <summary> /// Convert entity to XML node and append to Services node in XML document. /// </summary> /// <param name="entity">Entities to convert to XML and store in document.</param> private void Serialize(BaseEntity entity) { if(entity == null) { return; } var servicesNode = GetServicesNodeInOutputDoc(); var node = TransformEntityToNode(entity); servicesNode.AppendChild(node); if(entity is BaseServiceEntity) { var serviceEntity = (BaseServiceEntity) entity; if (serviceEntity.HasTimer) { var timerNode = TransformEntityToNode(serviceEntity.Timer); servicesNode.AppendChild(timerNode); } } }