/// <summary> /// Convert a value of the specified attribute to a string. /// </summary> /// <param name="in_element"></param> /// <param name="in_attribute_name"></param> /// <param name="in_attribute_type"></param> internal void ConvertAttributeToEnumProperty(XPathNavigator in_element, string in_attribute_name, int in_attribute_type = 0) { string value = in_element.GetAttribute(in_attribute_name, ""); // check if attribute exists if (!string.IsNullOrEmpty(value)) { Type class_type = this.GetType(); PropertyInfo property_info = class_type.GetProperty(in_attribute_name); // convert string to enum try { var enum_buffer = Enum.Parse(property_info.PropertyType, value, true); property_info.SetValue(this, enum_buffer, null); } catch { // throw an exception if value is invalid IXmlLineInfo info = (IXmlLineInfo)in_element; ParserException exception = new ParserException(info.LineNumber, info.LinePosition); exception.SetInvalidAttributeValue(in_attribute_name); throw exception; } } else { // if attribute is obligatory throw an exception if ((in_attribute_type & atObligatory) != 0) { IXmlLineInfo info = (IXmlLineInfo)in_element; ParserException exception = new ParserException(info.LineNumber, info.LinePosition); exception.SetAttributeNotFoundError(in_attribute_name); throw exception; } } }
/// <summary> /// Converts attribute to a bool peroperty /// </summary> /// <param name="in_element"></param> /// <param name="in_attribute_name"></param> /// <param name="inout_success"></param> internal void ConvertAttributeToBoolProperty(XPathNavigator in_element, string in_attribute_name, int in_attribute_type) { string value = in_element.GetAttribute(in_attribute_name, ""); // check if attribute exists if (!string.IsNullOrEmpty(value)) { // get property information Type class_type = this.GetType(); PropertyInfo property_info = class_type.GetProperty(in_attribute_name); // convert string to bool bool bool_buffer = false; if (bool.TryParse(value.Trim(), out bool_buffer)) { property_info.SetValue(this, bool_buffer, null); } else { // throw an exception if value is invalid IXmlLineInfo info = (IXmlLineInfo)in_element; ParserException exception = new ParserException(info.LineNumber, info.LinePosition); exception.SetInvalidAttributeValue(in_attribute_name); throw exception; } } else { // if attribute is obligatory throw an exception if ((in_attribute_type & atObligatory) != 0) { IXmlLineInfo info = (IXmlLineInfo)in_element; ParserException exception = new ParserException(info.LineNumber, info.LinePosition); exception.SetAttributeNotFoundError(in_attribute_name); throw exception; } } }