/// <summary>
        /// Deserializes the XElement to the fundamental primitive (e.g. string, int etc.) of a specified type using options.
        /// </summary>
        /// <param name="tag">The tag.</param>
        /// <param name="parentElement">The parent XElement used to deserialize the fundamental primitive.</param>
        /// <param name="item">The item.</param>
        /// <param name="prop">The property.</param>
        /// <param name="options">The options.</param>
        /// <returns>The deserialized fundamental primitive from the XElement.</returns>
        public static object Deserialize(IDFeElement tag, XObject parentElement, object item, PropertyInfo prop, SerializerOptions options, int idx = -1)
        {
            if (parentElement == null)
            {
                return(null);
            }

            var element = parentElement as XElement;
            var value   = element?.Value ?? ((XAttribute)parentElement).Value;

            return(GetValue(tag.Tipo, value, item, prop));
        }
        /// <summary>
        /// Serializes a fundamental primitive object (e.g. string, int etc.) into a XElement using options.
        /// </summary>
        /// <param name="tag">The name of the primitive to serialize.</param>
        /// <param name="item">The item.</param>
        /// <param name="prop">The property.</param>
        /// <param name="options">Indicates how the output is formatted or serialized.</param>
        /// <returns>The XElement representation of the primitive.</returns>
        public static XObject Serialize(IDFeElement tag, object item, PropertyInfo prop, SerializerOptions options, int idx = -1)
        {
            try
            {
                var value              = prop.GetValueOrIndex(item, idx);
                var estaVazio          = value == null || value.ToString().IsEmpty();
                var conteudoProcessado = ProcessValue(ref estaVazio, tag.Tipo, value, tag.Ocorrencia, tag.Min, prop, item);

                string alerta;
                if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio && tag.Min > 0)
                {
                    alerta = DFeSerializer.ErrMsgVazio;
                }
                else
                {
                    alerta = string.Empty;
                }

                if (conteudoProcessado.IsEmpty() && conteudoProcessado.Length < tag.Min && alerta.IsEmpty() &&
                    conteudoProcessado.Length > 1)
                {
                    alerta = DFeSerializer.ErrMsgMenor;
                }

                if (!string.IsNullOrEmpty(conteudoProcessado.Trim()) && conteudoProcessado.Length > tag.Max)
                {
                    alerta = DFeSerializer.ErrMsgMaior;
                }

                if (!string.IsNullOrEmpty(alerta.Trim()) && DFeSerializer.ErrMsgVazio.Equals(alerta) && !estaVazio)
                {
                    alerta += $" [{value}]";
                }

                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, alerta);

                XObject xmlTag = null;
                if (tag.Ocorrencia == Ocorrencia.Obrigatoria && estaVazio)
                {
                    xmlTag = tag is DFeElementAttribute ? (XObject) new XElement(tag.Name) : new XAttribute(tag.Name, "");
                }

                if (estaVazio)
                {
                    return(xmlTag);
                }

                var elementValue = options.RemoverAcentos ? conteudoProcessado.RemoveAccent() : conteudoProcessado;
                elementValue = options.RemoverEspacos ? elementValue.Trim() : elementValue;

                if (tag is DFeAttributeAttribute)
                {
                    return(new XAttribute(tag.Name, elementValue));
                }

                var cData = ((DFeElementAttribute)tag).UseCData;
                if (!elementValue.IsCData())
                {
                    return(cData ? new XElement(tag.Name, new XCData(elementValue)) :
                           new XElement(tag.Name, elementValue));
                }

                elementValue = elementValue.RemoveCData();
                return(new XElement(tag.Name, new XCData(elementValue)));
            }
            catch (Exception ex)
            {
                options.AddAlerta(tag.Id, tag.Name, tag.Descricao, ex.ToString());
                return(null);
            }
        }