protected internal virtual bool SerializeElement(XmlWriter writer, bool serializeCollectionKey) {
PreSerialize(writer);
bool DataToWrite = _bDataToWrite;
bool hasAnyChildElements = false;
bool foundDefaultElement = false;
ConfigurationPropertyCollection props = Properties;
ConfigurationPropertyCollection collectionKeys = null;
for (int index = 0; index < _values.Count; index++) {
string key = _values.GetKey(index);
object value = _values[index];
ConfigurationProperty prop = (ConfigurationProperty)props[key];
if (prop == null || (collectionKeys != null && !collectionKeys.Contains(prop.Name))) {
continue;
}
if (typeof(ConfigurationElement).IsAssignableFrom(prop.Type)) {
hasAnyChildElements = true;
}
else {
if ((_lockedAllExceptAttributesList != null && _lockedAllExceptAttributesList.HasParentElements && !_lockedAllExceptAttributesList.DefinedInParent(prop.Name)) ||
(_lockedAttributesList != null && _lockedAttributesList.DefinedInParent(prop.Name))) {
if (prop.IsRequired == true)
throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_required_attribute_locked, prop.Name));
value = s_nullPropertyValue;
}
if (value != s_nullPropertyValue) {
if (serializeCollectionKey == false || prop.IsKey == true) {
string xmlValue = null;
// If this was an invalid string value and was cached - write it out as is
if (value is InvalidPropValue) {
xmlValue = ((InvalidPropValue)value).Value;
}
else {
prop.Validate(value);
xmlValue = prop.ConvertToString(value);
}
if ((xmlValue != null) && (writer != null)) {
writer.WriteAttributeString(prop.Name, xmlValue);
}
DataToWrite = DataToWrite || (xmlValue != null);
}
}
}
}
if (serializeCollectionKey == false) {
DataToWrite |= SerializeLockList(_lockedAttributesList, LockAttributesKey, writer);
DataToWrite |= SerializeLockList(_lockedAllExceptAttributesList, LockAllAttributesExceptKey, writer);
DataToWrite |= SerializeLockList(_lockedElementsList, LockElementsKey, writer);
DataToWrite |= SerializeLockList(_lockedAllExceptElementsList, LockAllElementsExceptKey, writer);
if ((_fItemLocked & ConfigurationValueFlags.Locked) != 0 &&
(_fItemLocked & ConfigurationValueFlags.Inherited) == 0 &&
(_fItemLocked & ConfigurationValueFlags.XMLParentInherited) == 0) {
DataToWrite = true;
if (writer != null)
writer.WriteAttributeString(LockItemKey, true.ToString().ToLower(CultureInfo.InvariantCulture));
}
}
if (hasAnyChildElements) {
for (int index = 0; index < _values.Count; index++) {
string key = _values.GetKey(index);
object value = _values[index];
ConfigurationProperty prop = (ConfigurationProperty)props[key];
// if we are writing a remove and the sub element is not part of the key don't write it.
if (serializeCollectionKey == false || prop.IsKey == true) {
if (value is ConfigurationElement) {
if (!((_lockedElementsList != null && _lockedElementsList.DefinedInParent(key)) ||
(_lockedAllExceptElementsList != null && _lockedAllExceptElementsList.HasParentElements && !_lockedAllExceptElementsList.DefinedInParent(key)))) {
ConfigurationElement elem = (ConfigurationElement)value;
if (prop.Name != ConfigurationProperty.DefaultCollectionPropertyName) {
DataToWrite |= elem.SerializeToXmlElement(writer, prop.Name);
}
else if (!foundDefaultElement) {
// Prevent the locks from serializing a second time since locks
// on a default collection serialize with their parent node
elem._lockedAttributesList = null;
elem._lockedAllExceptAttributesList = null;
elem._lockedElementsList = null;
elem._lockedAllExceptElementsList = null;
DataToWrite |= elem.SerializeElement(writer, false);
foundDefaultElement = true;
}
else {
throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_element_cannot_have_multiple_child_elements, prop.Name));
}
}
}
}
}
}
return DataToWrite;
}
private bool SerializeLockList(ConfigurationLockCollection list, String elementKey, XmlWriter writer) {