public static XObject[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options) { XElement arrayElement = null; if (tag.IsValue) { arrayElement = new XElement(tag.Name); } var childElements = new List <XElement>(); foreach (var value in values) { var itemTag = itemTags.SingleOrDefault(x => x.Tipo == value.GetType()); Guard.Against <ACBrDFeException>(itemTag == null, $"Item {value.GetType().Name} não presente na lista de itens."); XElement childElement; if (itemTag.IsValue) { var properties = value.GetType().GetPropsAndValidate(value); var valueProp = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>()); var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); XNamespace aw = itemTag.Namespace ?? string.Empty; childElement = new XElement(aw + itemTag.Name); var childValue = valueProp.GetValueOrIndex(value); var estaVazio = childValue == null || childValue.ToString().IsEmpty(); childElement.Value = PrimitiveSerializer.ProcessValue(ref estaVazio, valueAtt.Tipo, valueProp, valueAtt.Ocorrencia, valueAtt.Min, valueProp, value); foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>())) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); var att = (XAttribute)PrimitiveSerializer.Serialize(attTag, value, property, options); childElement.AddAttribute(att); } } else { childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag.Name, itemTag.Namespace, options); } childElements.Add(childElement); } if (!tag.IsValue) { arrayElement.AddChild(childElements.ToArray()); } return(tag.IsValue ? childElements.ToArray() : new XObject[] { arrayElement }); }
public static XObject[] SerializeChild(ICollection values, DFeCollectionAttribute tag, DFeItemAttribute[] itemTags, SerializerOptions options) { var arrayElement = new XElement(tag.Name); foreach (var value in values) { var itemTag = itemTags.SingleOrDefault(x => x.Tipo == value.GetType()); Guard.Against <ACBrDFeException>(itemTag == null, $"Item {value.GetType().Name} não presente na lista de itens."); XElement childElement; if (itemTag.IsValue) { var properties = value.GetType().GetProperties() .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(value)) .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray(); Guard.Against <ACBrDFeException>(!properties.All(x => x.HasAttribute <DFeItemValueAttribute>() || x.HasAttribute <DFeAttributeAttribute>()), $"Item {value.GetType().Name} é do tipo [ItemValue] e so pode ter atributo do tipo [DFeAttributeAttribute] ou [DFeItemValueAttribute]."); Guard.Against <ACBrDFeException>(properties.Count(x => x.HasAttribute <DFeItemValueAttribute>()) != 1, $"Item {value.GetType().Name} é do tipo [ItemValue] e não tem presente o atributo [DFeItemValueAttribute] ou possui mais de um atributo."); var valueProp = properties.SingleOrDefault(x => x.HasAttribute <DFeItemValueAttribute>()); var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); XNamespace aw = itemTag.Namespace ?? string.Empty; childElement = new XElement(aw + itemTag.Name); var childValue = valueProp.GetValueOrIndex(value); var estaVazio = childValue == null || childValue.ToString().IsEmpty(); childElement.Value = PrimitiveSerializer.ProcessValue(ref estaVazio, valueAtt.Tipo, valueProp, valueAtt.Ocorrencia, valueAtt.Min, valueProp, value); foreach (var property in properties.Where(x => x.HasAttribute <DFeAttributeAttribute>())) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); var att = (XAttribute)PrimitiveSerializer.Serialize(attTag, value, property, options); childElement.AddAttribute(att); } } else { childElement = ObjectSerializer.Serialize(value, value.GetType(), itemTag.Name, itemTag.Namespace, options); } arrayElement.AddChild(childElement); } return(new XObject[] { arrayElement }); }
public static XElement Serialize(string name, string nameSpace, object parentObject, SerializerOptions options, PropertyInfo valueProp, PropertyInfo[] attProps) { XNamespace aw = nameSpace; var element = nameSpace.IsEmpty() ? new XElement(name) : new XElement(aw + name); var valueAtt = valueProp.GetAttribute <DFeItemValueAttribute>(); var childValue = valueProp.GetValueOrIndex(parentObject); var estaVazio = childValue == null || childValue.ToString().IsEmpty(); element.Value = PrimitiveSerializer.ProcessValue(ref estaVazio, valueAtt.Tipo, childValue, valueAtt.Ocorrencia, valueAtt.Min, valueProp, parentObject); foreach (var property in attProps) { var attTag = property.GetAttribute <DFeAttributeAttribute>(); var att = (XAttribute)PrimitiveSerializer.Serialize(attTag, parentObject, property, options); element.AddAttribute(att); } return(element); }