コード例 #1
0
        static public string ConvertAttributeToString(XPathNavigator in_element, string in_attribute_name, int in_attribute_type)
        {
            string attribute = in_element.GetAttribute(in_attribute_name, "");

            if (string.IsNullOrEmpty(attribute) && (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(attribute);
            }
        }
コード例 #2
0
        /// <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 ConvertAttributeToInt(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) && (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
            {
                // convert string to int
                int int_buffer = 0;
                int_buffer = int.Parse(attribute.Trim());

                return(int_buffer);
            }
        }
コード例 #3
0
        /// <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);
                }
            }
        }
コード例 #4
0
            /// <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));
                    }
                }
            }