public static XElement Serialize(object value, Type tipo, string name, string nameSpace, SerializerOptions options)
        {
            try
            {
                XNamespace aw            = nameSpace ?? string.Empty;
                var        objectElement = nameSpace.IsEmpty() ? new XElement(name) : new XElement(aw + name);

                var properties = tipo.GetProperties()
                                 .Where(x => !x.ShouldIgnoreProperty() && x.ShouldSerializeProperty(value))
                                 .OrderBy(x => x.GetAttribute <DFeBaseAttribute>()?.Ordem ?? 0).ToArray();

                foreach (var prop in properties)
                {
                    var elements = Serialize(prop, value, options);
                    if (elements == null)
                    {
                        continue;
                    }

                    objectElement.AddChilds(elements.ToArray());
                }

                return(objectElement);
            }
            catch (Exception e)
            {
                var msg = $"Erro ao serializar o objeto:{Environment.NewLine}{value}";
                logger.Error(msg, e);
                throw new ACBrDFeException(msg, e);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Serializes the specified object to a XElement using options.
        /// </summary>
        /// <param name="value">The object to serialize.</param>
        /// <param name="tipo">The tipo.</param>
        /// <param name="name">The name of the object to serialize.</param>
        /// <param name="nameSpace"></param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <returns>The XElement representation of the object.</returns>
        /// <exception cref="ACBrDFeException"></exception>
        public static XElement Serialize(object value, Type tipo, string name, string nameSpace, SerializerOptions options)
        {
            try
            {
                XNamespace aw            = nameSpace ?? string.Empty;
                var        objectElement = new XElement(aw + name);

                var properties = tipo.GetProperties();
                foreach (var prop in properties)
                {
                    if (prop.ShouldIgnoreProperty() || !prop.ShouldSerializeProperty(value))
                    {
                        continue;
                    }

                    var elements = Serialize(prop, value, options);
                    if (elements == null)
                    {
                        continue;
                    }

                    foreach (var element in elements)
                    {
                        var child = element as XElement;
                        if (child != null)
                        {
                            objectElement.AddChild(child);
                        }
                        else
                        {
                            objectElement.AddAttribute((XAttribute)element);
                        }
                    }
                }

                return(objectElement);
            }
            catch (Exception e)
            {
                var msg = $"Erro ao serializar o objeto:{Environment.NewLine}{value}";
                Logger.Error(msg, e);
                throw new ACBrDFeException(msg, e);
            }
        }