System.Configuration.ConfigurationElementCollection.OnDeserializeUnrecognizedElement C# (CSharp) Метод

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

protected OnDeserializeUnrecognizedElement ( String elementName, XmlReader reader ) : bool
elementName String
reader System.Xml.XmlReader
Результат bool
        protected override bool OnDeserializeUnrecognizedElement(String elementName, XmlReader reader) {
            bool handled = false; // 
            if (CollectionType == ConfigurationElementCollectionType.AddRemoveClearMap ||
                CollectionType == ConfigurationElementCollectionType.AddRemoveClearMapAlternate) {
                if (elementName == _addElement) {
                    ConfigurationElement elem = CallCreateNewElement();
                    elem.ResetLockLists(this);
                    elem.DeserializeElement(reader, false);
                    BaseAdd(elem);
                    handled = true;
                }
                else if (elementName == _removeElement) {
                    ConfigurationElement elem = CallCreateNewElement();
                    elem.ResetLockLists(this);
                    elem.DeserializeElement(reader, true);
                    if (IsElementRemovable(elem) == true) {
                        BaseRemove(GetElementKeyInternal(elem), false);
                    }

                    handled = true;
                }
                else if (elementName == _clearElement) {
                    if (reader.AttributeCount > 0) {
                        while (reader.MoveToNextAttribute()) {
                            String propertyName = reader.Name;
                            throw new ConfigurationErrorsException(SR.GetString(SR.Config_base_unrecognized_attribute, propertyName), reader);
                        }
                    }
                    CheckLockedElement(elementName, reader);
                    reader.MoveToElement();
                    BaseClear(); // 
                    bEmitClearTag = true;
                    handled = true;
                }
            }
            else if (elementName == ElementName) {
                if (BaseConfigurationRecord.IsReservedAttributeName(elementName)) {
                    throw new ArgumentException(SR.GetString(SR.Basicmap_item_name_reserved, elementName));
                }
                ConfigurationElement elem = CallCreateNewElement();
                elem.ResetLockLists(this);
                elem.DeserializeElement(reader, false);
                BaseAdd(elem);

                handled = true;
            }
            else if (IsElementName(elementName)) {   // this section handle the collection like the allow deny senario which
                if (BaseConfigurationRecord.IsReservedAttributeName(elementName)) {
                    throw new ArgumentException(SR.GetString(SR.Basicmap_item_name_reserved, elementName));
                }
                // have multiple tags for the collection
                ConfigurationElement elem = CallCreateNewElement(elementName);
                elem.ResetLockLists(this);
                elem.DeserializeElement(reader, false);
                BaseAdd(-1, elem);
                handled = true;
            }
            return handled;
        }

Usage Example

Пример #1
0
        protected internal virtual void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            Hashtable readProps = new Hashtable();

            reader.MoveToContent();
            elementPresent = true;

            while (reader.MoveToNextAttribute())
            {
                PropertyInformation prop = ElementInformation.Properties [reader.LocalName];
                if (prop == null || (serializeCollectionKey && !prop.IsKey))
                {
                    /* handle the built in ConfigurationElement attributes here */
                    if (reader.LocalName == "lockAllAttributesExcept")
                    {
                        LockAllAttributesExcept.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockAllElementsExcept")
                    {
                        LockAllElementsExcept.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockAttributes")
                    {
                        LockAttributes.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockElements")
                    {
                        LockElements.SetFromList(reader.Value);
                    }
                    else if (reader.LocalName == "lockItem")
                    {
                        LockItem = (reader.Value.ToLowerInvariant() == "true");
                    }
                    else if (reader.LocalName == "xmlns")
                    {
                        /* ignore */
                    }
                    else if (this is ConfigurationSection && reader.LocalName == "configSource")
                    {
                        /* ignore */
                    }
                    else if (!OnDeserializeUnrecognizedAttribute(reader.LocalName, reader.Value))
                    {
                        throw new ConfigurationErrorsException("Unrecognized attribute '" + reader.LocalName + "'.", reader);
                    }

                    continue;
                }

                if (readProps.ContainsKey(prop))
                {
                    throw new ConfigurationErrorsException("The attribute '" + prop.Name + "' may only appear once in this element.", reader);
                }

                string value = null;
                try {
                    value = reader.Value;
                    ValidateValue(prop.Property, value);
                    prop.SetStringValue(value);
                } catch (ConfigurationErrorsException) {
                    throw;
                } catch (ConfigurationException) {
                    throw;
                } catch (Exception ex) {
                    string msg = String.Format("The value for the property '{0}' is not valid. The error is: {1}", prop.Name, ex.Message);
                    throw new ConfigurationErrorsException(msg, reader);
                }
                readProps [prop] = prop.Name;

                ConfigXmlTextReader _reader = reader as ConfigXmlTextReader;
                if (_reader != null)
                {
                    prop.Source     = _reader.Filename;
                    prop.LineNumber = _reader.LineNumber;
                }
            }

            reader.MoveToElement();
            if (reader.IsEmptyElement)
            {
                reader.Skip();
            }
            else
            {
                int depth = reader.Depth;

                reader.ReadStartElement();
                reader.MoveToContent();

                do
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        reader.Skip();
                        continue;
                    }

                    PropertyInformation prop = ElementInformation.Properties [reader.LocalName];
                    if (prop == null || (serializeCollectionKey && !prop.IsKey))
                    {
                        if (!OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                        {
                            if (prop == null)
                            {
                                ConfigurationElementCollection c = GetDefaultCollection();
                                if (c != null && c.OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                                {
                                    continue;
                                }
                            }
                            throw new ConfigurationErrorsException("Unrecognized element '" + reader.LocalName + "'.", reader);
                        }
                        continue;
                    }

                    if (!prop.IsElement)
                    {
                        throw new ConfigurationErrorsException("Property '" + prop.Name + "' is not a ConfigurationElement.");
                    }

                    if (readProps.Contains(prop))
                    {
                        throw new ConfigurationErrorsException("The element <" + prop.Name + "> may only appear once in this section.", reader);
                    }

                    ConfigurationElement val = (ConfigurationElement)prop.Value;
                    val.DeserializeElement(reader, serializeCollectionKey);
                    readProps [prop] = prop.Name;

                    if (depth == reader.Depth)
                    {
                        reader.Read();
                    }
                } while (depth < reader.Depth);
            }

            modified = false;

            foreach (PropertyInformation prop in ElementInformation.Properties)
            {
                if (!String.IsNullOrEmpty(prop.Name) && prop.IsRequired && !readProps.ContainsKey(prop))
                {
                    PropertyInformation p = ElementInformation.Properties [prop.Name];
                    if (p == null)
                    {
                        object val = OnRequiredPropertyNotFound(prop.Name);
                        if (!object.Equals(val, prop.DefaultValue))
                        {
                            prop.Value      = val;
                            prop.IsModified = false;
                        }
                    }
                }
            }

            PostDeserialize();
        }