Exemplo n.º 1
0
        void ParseProperties(XmlElement element, object obj)
        {
            XmlAttributeCollection attributes = element.Attributes;
            Type type = obj.GetType();

            PropertyInfo[] properties = type.GetProperties();

            // loop for each attribute defined in the given element and attempt to
            // set the value of a property whose name is equals to the element name.
            foreach (XmlAttribute att in attributes)
            {
                string property_name  = att.Name;
                string property_value = att.Value;

                // If desired remove the "-" character from the attribute name.
                if (remove_hyphen_from_attribute_names_)
                {
                    property_name = property_name.Replace("-", "");
                }

                PropertyInfo property;
                if (TryGetProperty(properties, property_name, out property) &&
                    property.CanWrite)
                {
                    Type property_type = property.PropertyType;
                    if (property_type.Name == "String")
                    {
                        property.SetValue(obj, property_value, null);
                    }
                    else if (property_type.IsValueType)
                    {
                        // try to convert the attribute value to the type of the property
                        System.ValueType value;
                        if (ValueTypes.TryParse(property_type, property_value, out value))
                        {
                            property.SetValue(obj, value, null);
                        }
                    }
                }
            }
        }