/// <summary> /// Coverts XML attribute value to integer using a default value /// </summary> /// <param name="in_element"></param> /// <param name="in_attribute_name"></param> /// <param name="in_attribute_type"></param> /// <param name="in_default_value"></param> /// <returns></returns> static public int ConvertAttributeToIntDefault(XPathNavigator in_element, string in_attribute_name, int in_attribute_type, int in_default_value) { string attribute = in_element.GetAttribute(in_attribute_name, ""); // check if attribute exists if (!string.IsNullOrEmpty(attribute)) { // convert string to int int int_buffer = 0; if (int.TryParse(attribute.Trim(), out int_buffer)) { return(int_buffer); } else { // throw an exception if value is invalid IXmlLineInfo info = (IXmlLineInfo)in_element; XMLParserException exception = new XMLParserException(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; XMLParserException exception = new XMLParserException(info.LineNumber, info.LinePosition); exception.SetAttributeNotFoundError(in_attribute_name); throw exception; } else { return(in_default_value); } } }
/// <summary> /// Converts string (attribute value) to enum or any object /// </summary> /// <param name="in_element"></param> /// <param name="in_attribute_name"></param> /// <param name="in_attribute_type"></param> /// <returns></returns> public T ConvertAttributeToEnum(XPathNavigator in_element, string in_attribute_name, int in_attribute_type) { string attribute = in_element.GetAttribute(in_attribute_name, ""); // check if attribute exists if (!string.IsNullOrEmpty(attribute)) { // convert to enum try { return(this[attribute]); } catch { // throw an exception if attribute value is invalid IXmlLineInfo info = (IXmlLineInfo)in_element; XMLParserException exception = new XMLParserException(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; XMLParserException exception = new XMLParserException(info.LineNumber, info.LinePosition); exception.SetAttributeNotFoundError(in_attribute_name); throw exception; } else { return(default(T)); } } }