System.Configuration.ConfigurationElement.SerializeElement C# (CSharp) Метод

SerializeElement() защищенный Метод

protected SerializeElement ( XmlWriter writer, bool serializeCollectionKey ) : bool
writer System.Xml.XmlWriter
serializeCollectionKey bool
Результат bool
        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) {

Usage Example

        protected internal override bool SerializeElement(XmlWriter writer, bool serializeCollectionKey)
        {
            if (serializeCollectionKey)
            {
                return(base.SerializeElement(writer, serializeCollectionKey));
            }

            bool wroteData = false;

            if (IsBasic)
            {
                for (int n = 0; n < list.Count; n++)
                {
                    ConfigurationElement elem = (ConfigurationElement)list [n];
                    if (ElementName != string.Empty)
                    {
                        wroteData = elem.SerializeToXmlElement(writer, ElementName) || wroteData;
                    }
                    else
                    {
                        wroteData = elem.SerializeElement(writer, false) || wroteData;
                    }
                }
            }
            else
            {
                if (emitClear)
                {
                    writer.WriteElementString(clearElementName, "");
                    wroteData = true;
                }

                if (removed != null)
                {
                    for (int n = 0; n < removed.Count; n++)
                    {
                        writer.WriteStartElement(removeElementName);
                        ((ConfigurationElement)removed[n]).SerializeElement(writer, true);
                        writer.WriteEndElement();
                    }
                    wroteData = wroteData || removed.Count > 0;
                }

                for (int n = 0; n < list.Count; n++)
                {
                    ConfigurationElement elem = (ConfigurationElement)list [n];
                    elem.SerializeToXmlElement(writer, addElementName);
                }

                wroteData = wroteData || list.Count > 0;
            }
            return(wroteData);
        }