예제 #1
0
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool flag;

            if (value == null)
            {
                flag = true;
            }
            else
            {
                flag = (!removeIfEmpty ? false : string.IsNullOrEmpty(value));
            }
            bool           flag1 = flag;
            StageAttribute str   = parent.Attribute(name);

            if (str != null)
            {
                if (flag1)
                {
                    str.Remove();
                    return;
                }
                if (!str.isText)
                {
                    throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag1)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }
        /// <summary>
        /// Sets the value of a text attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        /// <param name="removeIfEmpty">if set to <c>true</c> the attribute is removed if the <paramref name="value"/> is empty. The attribute is always removed if <paramref name="value"/> is <c>null</c></param>
        public static void SetTextAttribute(this StageElement parent, string name, string value, bool removeIfEmpty = true)
        {
            bool remove = (value == null) || (removeIfEmpty && string.IsNullOrEmpty(value));

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (!attrib.isText)
            {
                throw new InvalidOperationException("Cannot set a text value on a non-text attribute.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
        /// <summary>
        /// Sets the value of an attribute. If it does not exist it is created.
        /// If the value is <c>null</c> the attribute is removed.
        /// If multiple identically named attributes exists only the first one is set.
        /// </summary>
        /// <param name="parent">The parent element.</param>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value to set.</param>
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool remove = (value == null);

            var attrib = parent.Attribute(name);

            if (attrib == null)
            {
                if (!remove)
                {
                    parent.Add(SerializationMaster.ToStageAttribute(name, value));
                }
            }
            else if (remove)
            {
                attrib.Remove();
            }
            else if (attrib.isText && !(value is string))
            {
                throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
            }
            else
            {
                attrib.value = SerializationMaster.ToString(value);
            }
        }
예제 #4
0
 public static T AttributeValue <T>(this StageElement element, string attributeName)
 {
     if (element != null)
     {
         StageAttribute stageAttribute = element.Attribute(attributeName);
         if (stageAttribute != null)
         {
             return(SerializationMaster.FromString <T>(stageAttribute.@value));
         }
     }
     throw new ArgumentException(string.Concat("No attribute by that name was found: ", attributeName));
 }
        /// <summary>
        /// Gets the converted value of the specified attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <returns>The value of the attribute.</returns>
        /// <exception cref="System.ArgumentException">If no attribute by that name was found.</exception>
        public static T AttributeValue <T>(this StageElement element, string attributeName)
        {
            if (element != null)
            {
                var attrib = element.Attribute(attributeName);
                if (attrib != null)
                {
                    return(SerializationMaster.FromString <T>(attrib.value));
                }
            }

            throw new ArgumentException("No attribute by that name was found: " + attributeName);
        }
예제 #6
0
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = null)
        {
            if (element == null)
            {
                return(defaultValue);
            }
            StageAttribute stageAttribute = element.Attribute(attributeName);

            if (stageAttribute == null)
            {
                return(defaultValue);
            }
            return(SerializationMaster.FromString <T>(stageAttribute.@value));
        }
        /// <summary>
        /// Gets the converted value of the specified attribute, or a default value if the element or the attribute is null or if it has no value.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="attributeName">Name of the attribute.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns>The value of the attribute, or <paramref name="defaultValue"/> if <paramref name="element"/> or the attribute is <c>null</c> or has no value.</returns>
        public static T AttributeValueOrDefault <T>(this StageElement element, string attributeName, T defaultValue = default(T))
        {
            if (element == null)
            {
                return(defaultValue);
            }

            var attrib = element.Attribute(attributeName);

            if (attrib == null)
            {
                return(defaultValue);
            }

            return(SerializationMaster.FromString <T>(attrib.value));
        }
예제 #8
0
        public static void SetAttribute(this StageElement parent, string name, object value)
        {
            bool           flag = value == null;
            StageAttribute str  = parent.Attribute(name);

            if (str != null)
            {
                if (flag)
                {
                    str.Remove();
                    return;
                }
                if (str.isText && !(value is string))
                {
                    throw new InvalidOperationException("Use SetTextAttribute to set text attributes.");
                }
                str.@value = SerializationMaster.ToString(value);
            }
            else if (!flag)
            {
                parent.Add(SerializationMaster.ToStageAttribute(name, value));
                return;
            }
        }