private void ParseAttribute(XAttribute attribute)
        {
            if (attribute.Name.NamespaceName == "http://www.w3.org/2000/xmlns/")
            {
                return;
            }

            var name = attribute.Name.LocalName;

            switch (name)
            {
            case "lockAllAttributesExcept":
                LockAllAttributesExcept.SetFromList(attribute.Value);
                return;

            case "lockAllElementsExcept":
                LockAllElementsExcept.SetFromList(attribute.Value);
                return;

            case "lockAttributes":
                LockAttributes.SetFromList(attribute.Value);
                return;

            case "lockElements":
                LockElements.SetFromList(attribute.Value);
                return;

            case "lockItem":
                IsLocked = attribute.Value;
                return;
            }

            RawAttributes.Add(name, attribute.Value);
            var child = Schema.AttributeSchemas[name];

            if (child == null && !Schema.AllowUnrecognizedAttributes)
            {
                throw new ArgumentException(String.Format("Unrecognized attribute '{0}'", name));
            }

            var item = new ConfigurationAttribute(name, child, attribute.Value, this);

            Attributes.Add(item);
        }
Exemple #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();
        }