Exemple #1
0
        public static LocatedNullable <bool> GetOptionalBool(
            this XElement elem, string name, bool?defaultValue = null)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                return(Located.Create(defaultValue));
            }

            if (attrib.Value == "true" || attrib.Value == "1")
            {
                return(Located.Create((bool?)true, attrib.GetValueLocation()));
            }
            if (attrib.Value == "false" || attrib.Value == "0")
            {
                return(Located.Create((bool?)false, attrib.GetValueLocation()));
            }

            throw CreateInvalidBoolValueException(attrib);
        }
Exemple #2
0
        public static LocatedRef <string> GetString(this XElement elem, string name)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                throw CreateMissingAttributeException(elem, name);
            }

            return(Located.Create(attrib.Value, attrib.GetValueLocation()));
        }
Exemple #3
0
        public static LocatedRef <string> GetCSymbol(
            this XElement elem, string name, string defaultValue = null)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                return(Located.Create(defaultValue));
            }

            return(Located.Create(attrib.Value, attrib.GetValueLocation()));
        }
Exemple #4
0
        public static LocatedVal <Guid> GetGuid(this XElement elem, string name)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                throw CreateMissingAttributeException(elem, name);
            }

            if (!Guid.TryParseExact(attrib.Value, "B", out var value))
            {
                throw CreateInvalidNumberValueException <byte>(attrib);
            }

            return(Located.Create(value, attrib.GetValueLocation()));
        }
Exemple #5
0
        public static LocatedVal <ulong> GetHexInt64(this XElement elem, string name)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                throw CreateMissingAttributeException(elem, name);
            }

            if (!TryParse(attrib.Value, NumberFormat.PrefixedHex, out ulong value))
            {
                throw CreateInvalidHexNumberValueException <ulong>(attrib);
            }

            return(Located.Create(value, attrib.GetValueLocation()));
        }
Exemple #6
0
        public static LocatedVal <uint> GetUInt32(this XElement elem, string name)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                throw CreateMissingAttributeException(elem, name);
            }

            if (!TryParse(attrib.Value, NumberFormat.HexDec, out uint value))
            {
                throw CreateInvalidNumberValueException <uint>(attrib);
            }

            return(Located.Create(value, attrib.GetValueLocation()));
        }
Exemple #7
0
        public static LocatedRef <QName> GetQName(this XElement elem, string name)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                throw CreateMissingAttributeException(elem, name);
            }

            return(Located.Create(
                       QName.Parse(attrib.Value, new XElementNamespaceResolver(elem)),
                       attrib.GetValueLocation()));
        }
Exemple #8
0
        public static LocatedNullable <Guid> GetOptionalGuid(
            this XElement elem, string name, Guid?defaultValue = null)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                return(Located.Create(defaultValue));
            }

            if (!Guid.TryParseExact(attrib.Value, "B", out var value))
            {
                throw CreateInvalidNumberValueException <byte>(attrib);
            }

            return(Located.Create((Guid?)value, attrib.GetValueLocation()));
        }
Exemple #9
0
        public static LocatedNullable <ushort> GetOptionalUInt16(
            this XElement elem, string name, ushort?defaultValue = null)
        {
            if (elem == null)
            {
                throw new ArgumentNullException(nameof(elem));
            }

            XAttribute attrib = elem.Attribute(name);

            if (attrib == null)
            {
                return(Located.Create(defaultValue));
            }

            if (!TryParse(attrib.Value, NumberFormat.HexDec, out ushort value))
            {
                throw CreateInvalidNumberValueException <ushort>(attrib);
            }

            return(Located.Create((ushort?)value, attrib.GetValueLocation()));
        }