/// <summary> /// Sets the value of a text element. If it does not exist it is created. /// If multiple identically named items exists only the first one is set. /// </summary> /// <param name="parent">The parent element.</param> /// <param name="name">The name of the item.</param> /// <param name="value">The value to set.</param> /// <param name="removeIfNullOrEmpty">If <c>true</c> and <para<paramref name="value"/> is <c>null</c> or empty, the item is removed rather than converted to a <see cref="StageNull"/></param> public static void SetTextValue(this StageElement parent, string name, string value, bool removeIfNullOrEmpty = true) { bool remove = (removeIfNullOrEmpty && string.IsNullOrEmpty(value)); var item = parent.Item(name); if (item == null) { if (!remove) { parent.Add(SerializationMaster.ToStageValue(name, value)); } return; } if (item is StageAttribute) { throw new InvalidOperationException("Use SetTextAttribute to set text attributes."); } var nullItem = item as StageNull; if (item != null) { if (value != null) { nullItem.Remove(); parent.Add(SerializationMaster.ToStageValue(name, value)); } return; } var valueItem = item as StageValue; if (item == null) { throw new InvalidOperationException("Only value elements can be set using this method."); } if (remove) { item.Remove(); } else if (value == null) { item.Remove(); parent.Add(new StageNull(name)); } else if (!valueItem.isText) { throw new InvalidOperationException("Cannot set a text value on a non-text value item."); } else { valueItem.value = SerializationMaster.ToString(value); } }
public static void AddTextValue(this StageContainer parent, string name, string value, bool onlyIfNotNullOrEmpty = true) { if (onlyIfNotNullOrEmpty && string.IsNullOrEmpty(value)) { return; } parent.Add(SerializationMaster.ToStageValue(name, value)); }
public static void SetValue(this StageElement parent, string name, object value, bool removeIfNull = true) { bool flag = (!removeIfNull ? false : value == null); StageItem stageItem = parent.Item(name); if (stageItem == null) { if (!flag) { parent.Add(SerializationMaster.ToStageValue(name, value)); } return; } if (stageItem is StageAttribute) { throw new InvalidOperationException("Use SetTextAttribute to set text attributes."); } StageNull stageNull = stageItem as StageNull; if (stageItem != null) { if (value != null) { stageNull.Remove(); parent.Add(SerializationMaster.ToStageValue(name, value)); } return; } StageValue str = stageItem as StageValue; if (stageItem == null) { throw new InvalidOperationException("Only value elements can be set using this method."); } if (flag) { stageItem.Remove(); return; } if (value == null) { stageItem.Remove(); parent.Add(new StageNull(name)); return; } if (str.isText && !(value is string)) { throw new InvalidOperationException("Use SetTextValue to set text values."); } str.@value = SerializationMaster.ToString(value); }