private void WriteArrayAsElement(object parent, ListPropertyDescriptor metadata, object entity, Type entityType, XmlTextWriter writer, XmlSerializerContext context) { if (!context.Settings.WriteEmptyLists) { IEnumerable list = (IEnumerable)entity; if (!list.GetEnumerator().MoveNext()) { return; } } string nodeName = metadata.GetElementNameForType(entityType, context, true); //escribo el inicio del nodo writer.WriteStartElement(nodeName); if (!context.Settings.UniqueSerializationForInstance || !context.Stack.ContainsInstance(entity)) { //agrego la lista a las entidades registradas long id = context.Stack.AddInstance(entity); //escribo el tipo, si corresponde base.WriteTypeDefinition(metadata, entityType, context, writer); //escribo el id del objeto si corresponde if (context.Settings.UniqueSerializationForInstance) { writer.WriteAttributeString(XmlSerializerSettings.ObjectIdAttributeName, id.ToString()); } IEnumerable list = (IEnumerable)entity; Type itemType; IXmlConverter itemConverter; //recorro todos los items foreach (object item in list) { if (item != null) { itemType = item.GetType(); itemConverter = context.GetConverter(itemType); ListItemPropertyDescriptor descriptor = metadata.GetItemPropertyDescriptor(context, false); itemConverter.ToXml(entity, descriptor, item, writer, context); } else { itemConverter = context.GetConverter(null); itemConverter.ToXml(entity, metadata, null, writer, context); } } } else { //me fijo si ya existe en el context long id = context.Stack.GetInstanceReferenceId(entity); writer.WriteAttributeString(XmlSerializerSettings.ObjectReferenceAttributeName, id.ToString()); } writer.WriteEndElement(); }
private void WriteProperties(PropertyDescriptor metadata, object entity, TypeDescriptor entityDescriptor, XmlTextWriter writer, XmlSerializerContext context, bool writeAttribute) { IXmlConverter propConverter; PropertyDescriptor desc; object value; bool mustWrite; //agrego la propiedades for (int i = 0; i < entityDescriptor.Properties.Count; i++) { desc = entityDescriptor.Properties[i]; value = desc.Metadata.GetValue(entity); if (value != null) { Type entityType = value.GetType(); if (writeAttribute) { mustWrite = entityDescriptor.IsAttributeProperty(desc, entityType); } else { mustWrite = entityDescriptor.IsElementProperty(desc, entityType); } if (mustWrite) { propConverter = context.GetConverter(entityType); propConverter.ToXml(entity, desc, value, writer, context); } } else { if (context.Settings.WriteNullValues) { if (entityDescriptor.CanWriteNullValue(desc.Metadata.PropertyName)) { if (writeAttribute) { mustWrite = entityDescriptor.IsAttributeNullValueProperty(desc.Metadata.PropertyName); } else { mustWrite = !entityDescriptor.IsAttributeNullValueProperty(desc.Metadata.PropertyName); } if (mustWrite) { propConverter = context.GetConverter(desc.Metadata.PropertyType); propConverter.ToXml(entity, desc, value, writer, context); } } } } } }
private void DeserializeKeyValueAtttributes(IDictionary parent, DictionaryKeyValuePropertyDescriptor keyDesc, DictionaryKeyValuePropertyDescriptor valueDesc, XmlReader reader, XmlSerializerContext context, out object key, out object value) { string attName; Type keyType; Type valueType; key = null; value = null; if (reader.MoveToFirstAttribute()) { attName = reader.Name; keyType = keyDesc.GetTypeFromAttributeName(attName, context); if (keyType == null) { valueType = valueDesc.GetTypeFromAttributeName(attName, context); if (valueType != null) { IXmlConverter converter = context.GetConverter(valueType); value = converter.FromXml(parent, valueDesc, valueType, reader, context); } } else { IXmlConverter converter = context.GetConverter(keyType); key = converter.FromXml(parent, keyDesc, keyType, reader, context); } if (reader.MoveToNextAttribute()) { attName = reader.Name; keyType = keyDesc.GetTypeFromAttributeName(attName, context); if (keyType == null) { valueType = valueDesc.GetTypeFromAttributeName(attName, context); if (valueType != null) { IXmlConverter converter = context.GetConverter(valueType); value = converter.FromXml(parent, valueDesc, valueType, reader, context); } } else { IXmlConverter converter = context.GetConverter(keyType); key = converter.FromXml(parent, keyDesc, keyType, reader, context); } } } //me ubico en el elemento.. reader.MoveToContent(); }
private void SerializeDictionaryItem(IDictionary dic, DictionaryEntry entry, DictionaryPropertyDescriptor propDesc, DictionaryKeyValuePropertyDescriptor keyDesc, DictionaryKeyValuePropertyDescriptor valueDesc, XmlTextWriter writer, XmlSerializerContext context) { //escribo el item string itemName = propDesc.GetElementNameForDictionaryItem(context); object key = entry.Key; object value = entry.Value; IXmlConverter converter; if (key != null) { writer.WriteStartElement(itemName); Type keyType = key.GetType(); if (keyDesc.IsXmlAttribute(keyType, context)) { converter = context.GetConverter(keyType); converter.ToXml(dic, keyDesc, key, writer, context); } if (value != null) { Type valueType = value.GetType(); if (valueDesc.IsXmlAttribute(valueType, context)) { converter = context.GetConverter(valueType); converter.ToXml(dic, valueDesc, value, writer, context); } } //escribo los elementos if (keyDesc.IsXmlElement(keyType, context)) { converter = context.GetConverter(keyType); converter.ToXml(dic, keyDesc, key, writer, context); } if (value != null) { Type valueType = value.GetType(); if (valueDesc.IsXmlElement(valueType, context)) { converter = context.GetConverter(valueType); converter.ToXml(dic, valueDesc, value, writer, context); } } writer.WriteEndElement(); } }
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); } }
private Type DeserializeItemsFromElements(Type entityType, IList parent, ListPropertyDescriptor metadata, XmlReader reader, XmlSerializerContext context) { ListItemPropertyDescriptor propDesc = metadata.GetItemPropertyDescriptor(context, false); //avanzo hasta la posicion del elemento string nodeName = reader.LocalName; //avanzo hasta el primer nodo bool end = !reader.Read(); XmlNodeType typeNode = reader.MoveToContent(); object association; Type itemType = null; //parseo las propiedades while (!end) { //me fijo si la propiedad es vacia if (reader.NodeType != XmlNodeType.EndElement) { //obtengo el tipo por el atributo itemType = base.GetEntityTypeForElement(propDesc, reader, context); if (itemType != null) { IXmlConverter converter = context.GetConverter(itemType); association = converter.FromXml(parent, propDesc, itemType, reader, context); if (association != null || context.Settings.AddNullValueInLists) { //agrego el item a la lista parent.Add(association); } } else { reader.Skip(); } } //avanzo... typeNode = reader.MoveToContent(); end = reader.NodeType == XmlNodeType.EndElement && reader.LocalName.Equals(nodeName); } reader.Read(); if (entityType.IsArray && itemType != null) { return(itemType); } else { return(metadata.DeclaringItemType); } }
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()); }
private void DeserializeElements(object parent, TypeDescriptor entityTypeDescriptor, XmlReader reader, XmlSerializerContext context) { string nodeName = reader.LocalName; //avanzo hasta la posicion del elemento bool end = !reader.Read(); XmlNodeType typeNode = reader.MoveToContent(); object association; PropertyDescriptor prop; //parseo las propiedades while (!end) { //me fijo si la propiedad es vacia if (reader.NodeType != XmlNodeType.EndElement) { prop = entityTypeDescriptor.GetPropertyByElementName(reader.LocalName); if (prop != null) { Type xmlType = base.GetEntityTypeForElement(prop, reader, context); if (xmlType != null) { IXmlConverter converter = context.GetConverter(xmlType); association = converter.FromXml(parent, prop, xmlType, reader, context); prop.Metadata.SetValue(parent, association); } else { reader.Skip(); } } else { //DefaultLogger.Warning("The xml element {0} doesn't match with a property of the class {1}", reader.LocalName, parent.GetType().ToString()); reader.Skip(); } } typeNode = reader.MoveToContent(); end = reader.NodeType == XmlNodeType.EndElement && reader.LocalName.Equals(nodeName); } reader.Read(); }
private void DeserializeAttributes(object parent, TypeDescriptor entityTypeDescriptor, XmlReader reader, XmlSerializerContext context) { //avanzo hasta el primer atributo bool isAtt = reader.MoveToFirstAttribute(); PropertyDescriptor prop; //si es un atributo, el tipo de la entidad coincide con el definido en la propiedad... string attName; object value; if (isAtt) { do { attName = reader.Name; prop = entityTypeDescriptor.GetPropertyByAttributeName(attName); if (prop != null) { //obtengo el tipo a partir del atributo Type propType = prop.GetTypeFromAttributeName(attName, context); IXmlConverter converter = context.GetConverter(propType); value = converter.FromXml(parent, prop, propType, reader, context); prop.Metadata.SetValue(parent, value); } else { //if (! (string.Compare(context.Settings.TypeSettings.AttributeTypeName, attName) == 0 // || string.Compare(XmlSerializerSettings.ObjectReferenceAttributeName, attName) == 0 // || string.Compare(XmlSerializerSettings.ObjectIdAttributeName, attName) == 0)) //DefaultLogger.Warning("The xml attribute {0} doesn't match with an property of the class {1}", attName, parent.GetType().ToString()); } }while (reader.MoveToNextAttribute()); } //me ubico de nuevo en el elemento reader.MoveToContent(); }
private void DeserializeKeyValueElements(IDictionary parent, DictionaryKeyValuePropertyDescriptor keyDesc, DictionaryKeyValuePropertyDescriptor valueDesc, XmlReader reader, XmlSerializerContext context, ref object key, ref object value) { string nodeName = reader.LocalName; //avanzo hasta la posicion del elemento bool end = !reader.Read(); XmlNodeType typeNode = reader.MoveToContent(); string elementName; Type keyType; Type valueType; //parseo las propiedades while (!end) { //me fijo si la propiedad es vacia if (reader.NodeType != XmlNodeType.EndElement) { elementName = reader.LocalName; keyType = keyDesc.GetTypeFromElementName(elementName, context); if (keyType == null) { valueType = valueDesc.GetTypeFromElementName(elementName, context); if (valueType != null) { IXmlConverter converter = context.GetConverter(valueType); value = converter.FromXml(parent, valueDesc, valueType, reader, context); } } else { IXmlConverter converter = context.GetConverter(keyType); key = converter.FromXml(parent, keyDesc, keyType, reader, context); } //ya esta parado sobre el siguiente elemento typeNode = reader.MoveToContent(); if (reader.NodeType != XmlNodeType.EndElement) { elementName = reader.LocalName; keyType = keyDesc.GetTypeFromElementName(elementName, context); if (keyType == null) { valueType = valueDesc.GetTypeFromElementName(elementName, context); if (valueType != null) { IXmlConverter converter = context.GetConverter(valueType); value = converter.FromXml(parent, valueDesc, valueType, reader, context); } } else { IXmlConverter converter = context.GetConverter(keyType); key = converter.FromXml(parent, keyDesc, keyType, reader, context); } } } typeNode = reader.MoveToContent(); end = reader.NodeType == XmlNodeType.EndElement && reader.LocalName.Equals(nodeName); } }