public static bool GetAttributeBoolValue(this XmlElement me, string name, XmlElementOption option = XmlElementOption.MustHaveValue, bool defaultValue = false)
        {
            XmlAttribute attrib = me.GetAttributeNode(name);

            if (attrib == null)
            {
                if (MustExist(option))
                {
                    throw new MissingAttributeException(me, name);
                }
                return(defaultValue);
            }
            if (MustHaveValue(option) && string.IsNullOrEmpty(attrib.Value))
            {
                throw new InvalidAttributeValueException(me, name);
            }
            if (string.IsNullOrEmpty(attrib.Value))
            {
                return(defaultValue);
            }
            try
            {
                return(bool.Parse(attrib.Value));
            }
            catch (Exception ex)
            {
                throw new InvalidAttributeValueException(me, name, ex);
            }
        }
        public static int GetAttributeIntValue(this XmlElement me, string name, XmlElementOption option = XmlElementOption.MustHaveValue, int defaultValue = 0)
        {
            XmlAttribute attrib = me.GetAttributeNode(name);

            if (attrib == null)
            {
                if (MustExist(option))
                {
                    throw new MissingAttributeException(me, name);
                }
                return(defaultValue);
            }
            if (MustHaveValue(option) && string.IsNullOrEmpty(attrib.Value))
            {
                throw new InvalidAttributeValueException(me, name);
            }
            if (string.IsNullOrEmpty(attrib.Value))
            {
                return(defaultValue);
            }
            int value;

            if (!int.TryParse(attrib.Value, NumberStyles.Integer, CultureInfo.InvariantCulture, out value))
            {
                throw new InvalidAttributeValueException(me, name);
            }
            return(value);
        }
Exemplo n.º 3
0
        public void GetAttributeIntValue(string xml, XmlElementOption option, int defaultValue, int expectedResult)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            int value = doc.DocumentElement.GetAttributeIntValue("value", option, defaultValue);

            Assert.That(value == expectedResult);
        }
Exemplo n.º 4
0
        public void GetAttributeBoolValue(string xml, XmlElementOption option, bool expectedResult)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            bool value = doc.DocumentElement.GetAttributeBoolValue("value", option);

            Assert.That(value == expectedResult);
        }
Exemplo n.º 5
0
//TODO        [TestCase("<Test />", XmlElementOption.MustExist, typeof(MissingAttributeException))]
        public void GetAttributeStringValueNegative(string xml, XmlElementOption option, Type expectedException)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            Assert.That(() =>
            {
                string name = doc.DocumentElement.GetAttributeStringValue("value");
            },
                        Throws.TypeOf(expectedException));
        }
Exemplo n.º 6
0
        public void GetAttributeStringValue(string xml, XmlElementOption option, string expectedResult)
        {
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(xml);

            string name = doc.DocumentElement.GetAttributeStringValue("value", option);

            if (expectedResult != null)
            {
                Assert.That(name.Equals(expectedResult));
            }
            else
            {
                Assert.That(name == null);
            }
        }
 public static bool MustHaveValue(XmlElementOption option)
 {
     return(option == XmlElementOption.MustHaveValue);
 }
        public static string GetAttributeStringValue(this XmlElement me, string name, XmlElementOption option = XmlElementOption.MustHaveValue)
        {
            XmlAttribute attrib = me.GetAttributeNode(name);

            if (attrib == null)
            {
                if (MustExist(option))
                {
                    //TODO add option to save attribute
                    return("");

                    throw new MissingAttributeException(me, name);
                }
                return(null);
            }
            if (MustHaveValue(option) && string.IsNullOrEmpty(attrib.Value))
            {
                throw new InvalidAttributeValueException(me, name);
            }
            return(attrib.Value);
        }