예제 #1
0
        public static bool TryRead <T>(XmlNode parentNode, string attributeName, Converter <string, T> converter, ref T value)
        {
            ArgumentHelper.AssertNotNull <XmlNode>(parentNode, "parentNode");
            ArgumentHelper.AssertNotEmpty(attributeName, "attributeName");
            ArgumentHelper.AssertNotNull <Converter <string, T> >(converter, "converter");

            XmlAttribute attr = parentNode.Attributes[attributeName];

            if (null == attr)
            {
                return(false);
            }
            else
            {
                value = converter(attr.Value);
                return(true);
            }
        }
예제 #2
0
        public static T Read <T>(XmlNode parentNode, string attributeName, Converter <string, T> converter, T defaultValue, bool throwExceptionIfMissing)
        {
            ArgumentHelper.AssertNotNull <XmlNode>(parentNode, "parentNode");
            ArgumentHelper.AssertNotEmpty(attributeName, "attributeName");
            ArgumentHelper.AssertNotNull <Converter <string, T> >(converter, "converter");

            XmlAttribute attr = parentNode.Attributes[attributeName];

            if (null == attr)
            {
                if (throwExceptionIfMissing)
                {
                    throw new ConfigurationErrorsException("Missing @" + attributeName + " attribute. This value should is required.", parentNode);
                }
                else
                {
                    return(defaultValue);
                }
            }
            else
            {
                return(converter(attr.Value));
            }
        }