示例#1
0
        private void DeserializeItems(Type entityType, Type itemType, IList parent, ListPropertyDescriptor metadata, ListItemPropertyDescriptor propDesc, string itemsStr, XmlSerializerContext context, bool isAttribute)
        {
            itemsStr = itemsStr.Trim();

            if (!TypeHelper.IsValueType(itemType))
            {
                throw new Exception("El item de tipo " + itemType.Name + " no es de tipo ValueType, no puede deserializarse de un atributo.");
            }

            ValueTypeConverter itemConverter = (ValueTypeConverter)context.GetConverter(itemType);

            //obtengo el tipo por el atributo
            ValueTypePropertyDescriptor itemDescriptor = propDesc.GetPropertyDescriptor <ValueTypePropertyDescriptor>(itemType, context);

            string itemSeparator = metadata.GetInlineItemSeparator(entityType, context, isAttribute);

            string[] items = itemsStr.Split(new string[] { itemSeparator }, StringSplitOptions.None);
            int      last  = items.Length;

            if (string.Compare(itemsStr[itemsStr.Length - 1].ToString(), itemSeparator) == 0)
            {
                last--;
            }

            object val;

            for (int i = 0; i < last; i++)
            {
                string item = items[i];
                val = itemConverter.GetValueFromString(itemDescriptor, item, itemType, context);
                parent.Add(val);
            }
        }
        public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            //le agrego los cdata si correspnode..

            string data        = attValue.ToString();
            bool   appendCData = false;

            int index = data.IndexOf('<');

            if (index > 0)
            {
                appendCData = true;
            }
            else
            {
                index       = data.IndexOf('>');
                appendCData = index > 0;
            }

            if (appendCData)
            {
                return("<![CDATA[" + data + "]]>");
            }
            else
            {
                return(data);
            }
        }
示例#3
0
        public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            string dateFormat = metadata.DateTimeFormat;

            if (dateFormat == null)
            {
                dateFormat = context.Settings.DefaultDateTimeFormat;
            }

            if (attValue is DateTime || attValue is DateTime?)
            {
                DateTime value = (DateTime)attValue;
                if (!string.IsNullOrEmpty(dateFormat))
                {
                    return(value.ToString(dateFormat));
                }
                else
                {
                    return(value.ToString(DateTimeFormatInfo.InvariantInfo));
                }
            }
            else
            {
                DateTimeOffset value = (DateTimeOffset)attValue;
                if (!string.IsNullOrEmpty(dateFormat))
                {
                    return(value.ToString(dateFormat));
                }
                else
                {
                    return(value.ToString(DateTimeFormatInfo.InvariantInfo));
                }
            }
        }
        protected override object DoFromXml(object parent, PropertyDescriptor metadata, Type entityType, XmlReader reader, XmlSerializerContext context)
        {
            string attributeName = null;
            string elementName   = null;
            string val           = null;
            object output        = null;
            ValueTypePropertyDescriptor valPropDesc = metadata.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context);

            //si el reader esta parado sobre un atributo.....entonces, el tipo lo obtengo a partir del atributo
            if (reader.NodeType == XmlNodeType.Attribute)
            {
                attributeName = reader.Name;
            }
            else
            {
                elementName   = reader.LocalName;
                attributeName = valPropDesc.GetAttributeNameForType(entityType, context);
            }

            if (string.IsNullOrEmpty(elementName))
            {
                //esta definido en el atributo, lo escribo
                string propAttName = valPropDesc.GetAttributeNameForType(entityType, context);

                if (string.Compare(attributeName, propAttName) == 0)
                {
                    val    = reader.Value;
                    output = this.GetValueFromString(valPropDesc.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context), val, entityType, context);
                }
            }
            else
            {
                if (string.IsNullOrEmpty(attributeName))
                {
                    val = reader.ReadElementContentAsString();
                }
                else
                {
                    if (reader.MoveToAttribute(attributeName))
                    {
                        //avanzo el elemento
                        val = reader.Value;
                        reader.Read();
                    }
                    else
                    {
                        //esta en el contenido del elemento
                        val = reader.ReadElementContentAsString();
                    }
                }

                output = this.GetValueFromString(valPropDesc, val, entityType, context);
            }

            return(output);
        }
示例#5
0
        protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
        {
            Type t = Type.GetType(attValue);

            if (t == null)
            {
                t = TypeConverter.GetSystemType(attValue);
            }
            return(t);
        }
示例#6
0
        private string GetInlineStringArray(ListPropertyDescriptor metadata, IEnumerable list, Type entityType, XmlSerializerContext context, bool isAttribute)
        {
            Type itemType    = null;
            Type currentType = null;

            ValueTypeConverter          itemConverter = null;
            StringBuilder               attBuilder    = new StringBuilder();
            ValueTypePropertyDescriptor valPropDesc   = null;

            string itemSeparator = metadata.GetInlineItemSeparator(entityType, context, isAttribute);
            bool   first         = true;

            foreach (object item in list)
            {
                if (item != null)
                {
                    if (itemType == null)
                    {
                        itemType = item.GetType();

                        if (!TypeHelper.IsValueType(itemType))
                        {
                            throw new Exception("El item de tipo " + itemType.Name + " no es de tipo ValueType, no puede serializarse en un atributo.");
                        }

                        itemConverter = (ValueTypeConverter)context.GetConverter(itemType);
                        ListItemPropertyDescriptor itemDesc = metadata.GetItemPropertyDescriptor(context, true);
                        valPropDesc = itemDesc.GetPropertyDescriptor <ValueTypePropertyDescriptor>(itemType, context);
                    }

                    currentType = item.GetType();

                    if (!currentType.Equals(itemType))
                    {
                        throw new Exception("El item de tipo " + currentType.Name + " es direferente al primer item del array de tipo " + itemType.Name + ". No es posible serializar un array de diferentes tipos en un atributo.");
                    }

                    string val = itemConverter.GetValueAsString(valPropDesc, item, itemType, context);

                    if (first)
                    {
                        first = false;
                    }
                    else
                    {
                        attBuilder.Append(itemSeparator);
                    }

                    attBuilder.Append(val);
                }
            }

            return(attBuilder.ToString());
        }
 protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
 {
     if (TypeHelper.IsNullableType(type))
     {
         Type enumType = TypeHelper.GetNullableType(type);
         return(Enum.Parse(enumType, attValue));
     }
     else
     {
         return(Enum.Parse(type, attValue));
     }
 }
示例#8
0
        internal static Exception CreateCanNotGetParseValueTypeException(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context, Exception inner)
        {
            string msg        = "No es posible parsear el valor desde un string. Valor: {0}, Tipo del Valor: {1}, Tipo Entidad: {2}, Propiedad: {3}";
            object entity     = context.Stack.InstanciesSequence.Peek();
            string entityType = "";

            if (entity != null)
            {
                entityType = entity.GetType().FullName;
            }

            return(new XmlDataSerializerException(string.Format(msg, attValue, type.FullName, entityType, metadata.Metadata.PropertyName), inner));
        }
        protected override void DoToXml(object parent, PropertyDescriptor propDescritor, object entity, XmlTextWriter writer, XmlSerializerContext context)
        {
            Type entityType = entity.GetType();
            ValueTypePropertyDescriptor metadata = propDescritor.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context);

            if (metadata.IsXmlContentText)
            {
                if (!TypeHelper.IsDefaultValue(entity, entityType) || context.Settings.WriteDefaultValues)
                {
                    string valueStr    = this.GetValueAsString(metadata, entity, entityType, context);
                    string elementName = metadata.GetElementNameForType(entityType, context, true);

                    //escribo el nombre del elemento...
                    writer.WriteStartElement(elementName);

                    //recorro el contenido
                    StringReader  stringReader = new StringReader(valueStr);
                    XmlTextReader reader       = new XmlTextReader(stringReader);

                    try
                    {
                        reader.MoveToContent();
                        //recorro todos los attributos y los escribo en el destino
                        if (reader.MoveToFirstAttribute())
                        {
                            writer.WriteAttributeString(reader.LocalName, reader.Value);
                            while (reader.MoveToNextAttribute())
                            {
                                writer.WriteAttributeString(reader.LocalName, reader.Value);
                            }
                        }

                        reader.MoveToContent();
                        string str = reader.ReadInnerXml();
                        writer.WriteRaw(str);
                    }
                    finally
                    {
                        stringReader.Close();
                        reader.Close();
                    }

                    //cierro el tag
                    writer.WriteEndElement();
                }
            }
            else
            {
                base.DoToXml(parent, metadata, entity, writer, context);
            }
        }
        public virtual string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            string value;

            if (type == _byte)
            {
                value = XmlConvert.ToString((Byte)attValue);
            }
            else if (type == _sbyte)
            {
                value = XmlConvert.ToString((SByte)attValue);
            }
            else if (type == _short)
            {
                value = XmlConvert.ToString((short)attValue);
            }
            else if (type == _int)
            {
                value = XmlConvert.ToString((Int32)attValue);
            }
            else if (type == _long)
            {
                value = XmlConvert.ToString((Int64)attValue);
            }
            else if (type == _ushort)
            {
                value = XmlConvert.ToString((UInt16)attValue);
            }
            else if (type == _uint)
            {
                value = XmlConvert.ToString((UInt32)attValue);
            }
            else if (type == _ulong)
            {
                value = XmlConvert.ToString((UInt64)attValue);
            }
            else if (type == _bool)
            {
                value = XmlConvert.ToString((bool)attValue);
            }
            else if (type == _char)
            {
                value = XmlConvert.ToString((char)attValue);
            }
            else
            {
                value = "";
            }

            return(value);
        }
        public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            string value;
            string numberFormat = metadata.NumberFormat;

            if (numberFormat == null)
            {
                numberFormat = context.Settings.DefaultNumberFormat;
            }

            if (type == _float || type == _nfloat)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = ((float)attValue).ToString(numberFormat, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = ((float)attValue).ToString(NumberFormatInfo.InvariantInfo);
                }
            }
            else if (type == _double || type == _ndouble)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = ((double)attValue).ToString(numberFormat, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = ((double)attValue).ToString(NumberFormatInfo.InvariantInfo);
                }
            }
            else if (type == _decimal || type == _ndecimal)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = ((decimal)attValue).ToString(numberFormat, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = ((decimal)attValue).ToString(NumberFormatInfo.InvariantInfo);
                }
            }
            else
            {
                value = "";
            }

            return(value);
        }
        protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
        {
            object value;
            string numberFormat = metadata.NumberFormat;

            if (numberFormat == null)
            {
                numberFormat = context.Settings.DefaultNumberFormat;
            }

            if (type == _float || type == _nfloat)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = float.Parse(attValue, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = float.Parse(attValue, CultureInfo.InvariantCulture);
                }
            }
            else if (type == _double || type == _ndouble)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = double.Parse(attValue, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = double.Parse(attValue, CultureInfo.InvariantCulture);
                }
            }
            else if (type == _decimal || type == _ndecimal)
            {
                if (!string.IsNullOrEmpty(numberFormat))
                {
                    value = decimal.Parse(attValue, CultureInfo.InvariantCulture);
                }
                else
                {
                    value = decimal.Parse(attValue, CultureInfo.InvariantCulture);
                }
            }
            else
            {
                value = null;
            }

            return(value);
        }
        protected override object DoFromXml(object parent, PropertyDescriptor propDesc, Type entityType, XmlReader reader, XmlSerializerContext context)
        {
            ValueTypePropertyDescriptor metadata = propDesc.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context);

            if (metadata.IsXmlContentText)
            {
                //tengo que leer todo el contenido...
                return(string.Intern(reader.ReadOuterXml()));
            }
            else
            {
                return(base.DoFromXml(parent, metadata, entityType, reader, context));
            }
        }
        protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
        {
            string timeSpanFormat = metadata.TimeSpanFormat;

            if (timeSpanFormat == null)
            {
                timeSpanFormat = context.Settings.DefaultTimeSpanFormat;
            }

            if (timeSpanFormat == null)
            {
                return(XmlConvert.ToTimeSpan(attValue));
            }
            else
            {
                return(TimeSpan.ParseExact(attValue, timeSpanFormat, null));
            }
        }
示例#15
0
        protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
        {
            string dateFormat = metadata.DateTimeFormat;

            if (dateFormat == null)
            {
                dateFormat = context.Settings.DefaultDateTimeFormat;
            }

            if (type.Equals(__dateTimeType) || type.Equals(__nullableDateTimeType))
            {
                return(XmlConvert.ToDateTime(attValue, dateFormat));
            }
            else
            {
                return(XmlConvert.ToDateTimeOffset(attValue, dateFormat));
            }
        }
示例#16
0
 public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
 {
     if (attValue == null)
     {
         return(null);
     }
     else
     {
         if (context.Settings.EnumAsString)
         {
             return(attValue.ToString());
         }
         else
         {
             return(((int)attValue).ToString());
         }
     }
 }
 public virtual object GetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
 {
     try
     {
         if (string.IsNullOrEmpty(attValue))
         {
             return(this.GetDefaultValue(metadata, type, context));
         }
         else
         {
             return(this.DoGetValueFromString(metadata, attValue, type, context));
         }
     }
     catch (Exception ex)
     {
         throw XmlDataSerializerExceptionFactory.CreateCanNotGetParseValueTypeException(metadata, attValue, type, context, ex);
     }
 }
        public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            string timeSpanFormat = metadata.TimeSpanFormat;

            if (timeSpanFormat == null)
            {
                timeSpanFormat = context.Settings.DefaultTimeSpanFormat;
            }

            TimeSpan value = (TimeSpan)attValue;

            if (timeSpanFormat == null)
            {
                return(value.ToString());
            }
            else
            {
                return(value.ToString(timeSpanFormat));
            }
        }
        protected override void DoToNullValueXml(object parent, PropertyDescriptor metadata, XmlTextWriter writer, XmlSerializerContext context)
        {
            Type entityType = metadata.Metadata.PropertyType;

            ValueTypePropertyDescriptor desc = metadata.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context);

            if (!desc.IsXmlElement(entityType, context))
            {
                string attributeName = metadata.GetAttributeNameForType(entityType, context);
                //esta definido el atributo, lo escribo
                writer.WriteAttributeString(attributeName, "");
            }
            else
            {
                string elementName = desc.GetElementNameForType(entityType, context, true);

                //escribo el nombre de la propiedad
                writer.WriteElementString(elementName, "");
            }
        }
        protected override void DoToXml(object parent, PropertyDescriptor metadata, object entity, XmlTextWriter writer, XmlSerializerContext context)
        {
            Type entityType = entity.GetType();

            if (!TypeHelper.IsDefaultValue(entity, entityType) || context.Settings.WriteDefaultValues)
            {
                ValueTypePropertyDescriptor desc = metadata.GetPropertyDescriptor <ValueTypePropertyDescriptor>(entityType, context);
                string valueStr = this.GetValueAsString(desc, entity, entityType, context);

                if (!desc.IsXmlElement(entityType, context))
                {
                    string attributeName = metadata.GetAttributeNameForType(entityType, context);
                    //esta definido el atributo, lo escribo
                    writer.WriteAttributeString(attributeName, valueStr);
                }
                else
                {
                    string elementName = desc.GetElementNameForType(entityType, context, true);

                    //escribo el nombre de la propiedad
                    writer.WriteStartElement(elementName);

                    base.WriteTypeDefinition(desc, entityType, context, writer);

                    if (desc.IsXmlAttribute(entityType, context))
                    {
                        string attributeName = metadata.GetAttributeNameForType(entityType, context);
                        //escribo el valor
                        writer.WriteAttributeString(attributeName, valueStr);
                    }
                    else
                    {
                        writer.WriteRaw(valueStr);
                    }
                    //writer.WriteString(valueStr); si tiene caracteres de xml los convierte a &amp

                    //cierro el tag
                    writer.WriteEndElement();
                }
            }
        }
示例#21
0
        public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
        {
            Guid guid = (Guid)attValue;

            return(guid.ToString());
        }
 protected override object GetDefaultValue(ValueTypePropertyDescriptor metadata, Type type, XmlSerializerContext context)
 {
     return(string.Empty);
 }
 protected virtual object GetDefaultValue(ValueTypePropertyDescriptor metadata, Type type, XmlSerializerContext context)
 {
     return(Activator.CreateInstance(type, true));
 }
        protected virtual object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
        {
            object value;

            if (type == _byte)
            {
                value = XmlConvert.ToByte(attValue);
            }
            else if (type == _sbyte)
            {
                value = XmlConvert.ToSByte(attValue);
            }
            else if (type == _short)
            {
                value = XmlConvert.ToInt16(attValue);
            }
            else if (type == _int)
            {
                value = XmlConvert.ToInt32(attValue);
            }
            else if (type == _long)
            {
                value = XmlConvert.ToInt64(attValue);
            }
            else if (type == _ushort)
            {
                value = XmlConvert.ToUInt16(attValue);
            }
            else if (type == _uint)
            {
                value = XmlConvert.ToUInt32(attValue);
            }
            else if (type == _ulong)
            {
                value = XmlConvert.ToUInt64(attValue);
            }
            else if (type == _bool)
            {
                value = XmlConvert.ToBoolean(attValue);
            }
            else if (type == _char)
            {
                value = XmlConvert.ToChar(attValue);
            }

            else if (type == _nbyte)
            {
                value = XmlConvert.ToByte(attValue);
            }
            else if (type == _nsbyte)
            {
                value = XmlConvert.ToSByte(attValue);
            }
            else if (type == _nshort)
            {
                value = XmlConvert.ToInt16(attValue);
            }
            else if (type == _nint)
            {
                value = XmlConvert.ToInt32(attValue);
            }
            else if (type == _nlong)
            {
                value = XmlConvert.ToInt64(attValue);
            }
            else if (type == _nushort)
            {
                value = XmlConvert.ToUInt16(attValue);
            }
            else if (type == _nuint)
            {
                value = XmlConvert.ToUInt32(attValue);
            }
            else if (type == _nulong)
            {
                value = XmlConvert.ToUInt64(attValue);
            }
            else if (type == _nbool)
            {
                value = XmlConvert.ToBoolean(attValue);
            }
            else if (type == _nchar)
            {
                value = XmlConvert.ToChar(attValue);
            }
            else
            {
                value = null;
            }

            return(value);
        }
 protected override object DoGetValueFromString(ValueTypePropertyDescriptor metadata, string attValue, Type type, XmlSerializerContext context)
 {
     return(attValue);
 }
示例#26
0
 public override string GetValueAsString(ValueTypePropertyDescriptor metadata, object attValue, Type type, XmlSerializerContext context)
 {
     return(this.TypeToString((Type)attValue, context.Settings.TypeSettings));
 }