DeserializeElement() protected method

protected DeserializeElement ( XmlReader reader, bool serializeCollectionKey ) : void
reader XmlReader
serializeCollectionKey bool
return void
        protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
        {
            if (IsBasic)
            {
                ConfigurationElement elem = null;

                if (elementName == ElementName)
                {
                    elem = CreateNewElementInternal(null);
                }
                if (IsElementName(elementName))
                {
                    elem = CreateNewElementInternal(elementName);
                }

                if (elem != null)
                {
                    elem.DeserializeElement(reader, false);
                    BaseAdd(elem);
                    modified = false;
                    return(true);
                }
            }
            else
            {
                if (elementName == clearElementName)
                {
                    reader.MoveToContent();
                    if (reader.MoveToNextAttribute())
                    {
                        throw new ConfigurationErrorsException("Unrecognized attribute '" + reader.LocalName + "'.");
                    }
                    reader.MoveToElement();
                    reader.Skip();
                    BaseClear();
                    emitClear = true;
                    modified  = false;
                    return(true);
                }
                else if (elementName == removeElementName)
                {
                    ConfigurationElement       elem       = CreateNewElementInternal(null);
                    ConfigurationRemoveElement removeElem = new ConfigurationRemoveElement(elem, this);
                    removeElem.DeserializeElement(reader, true);
                    BaseRemove(removeElem.KeyValue);
                    modified = false;
                    return(true);
                }
                else if (elementName == addElementName)
                {
                    ConfigurationElement elem = CreateNewElementInternal(null);
                    elem.DeserializeElement(reader, false);
                    BaseAdd(elem);
                    modified = false;
                    return(true);
                }
            }

            return(false);
        }
示例#2
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();
        }