コード例 #1
0
        /// <summary>
        /// Reads XML from the configuration file.
        /// </summary>
        /// <param name="reader">The XmlReader that reads from the configuration file.</param>
        /// <param name="serializeCollectionKey">true to serialize only the collection key properties; otherwise, false.</param>
        protected internal virtual void DeserializeElement(XmlReader reader, bool serializeCollectionKey)
        {
            Hashtable readProps = new Hashtable();

            reader.MoveToContent();

            while (reader.MoveToNextAttribute())
            {
                APGenPropertyInformation prop = ElementInformation.Properties[reader.LocalName];
                if (prop == null || (serializeCollectionKey && !prop.IsKey))
                {
                    if (reader.LocalName == "xmlns")
                    {
                        // Ignore
                    }
                    else
                    {
                        if (!OnDeserializeUnrecognizedAttribute(reader.LocalName, reader.Value))
                            throw new APGenException(APResource.GetString(APResource.APGen_UnrecognizedAttribute, reader.LocalName), reader);
                    }

                    continue;
                }

                if (readProps.ContainsKey(prop))
                    throw new APGenException(APResource.GetString(APResource.APGen_DuplicateAttribute, prop.Name), reader);

                string value = null;
                try
                {
                    value = reader.Value;
                    ValidateValue(prop.Property, value);
                    prop.SetStringValue(value);
                }
                catch (APGenException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new APGenException(APResource.GetString(APResource.APGen_PropertyCannotBeParsed, prop.Name), ex, reader);
                }
                readProps[prop] = prop.Name;
            }

            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;
                    }

                    APGenPropertyInformation prop = ElementInformation.Properties[reader.LocalName];
                    if (prop == null || (serializeCollectionKey && !prop.IsKey))
                    {
                        if (!OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                        {
                            if (prop == null)
                            {
                                APGenElementCollection collection = GetDefaultCollection();
                                if (collection != null && collection.OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                                    continue;
                            }
                            throw new APGenException(APResource.GetString(APResource.APGen_UnrecognizedElement, reader.LocalName), reader);
                        }
                        continue;
                    }

                    if (!prop.IsElement)
                        throw new APGenException(APResource.GetString(APResource.APGen_NotElement, prop.Name), reader);

                    if (readProps.Contains(prop))
                        throw new APGenException(APResource.GetString(APResource.APGen_DuplicateElement, prop.Name), reader);

                    APGenElement val = prop.Value as APGenElement;
                    val.DeserializeElement(reader, serializeCollectionKey);
                    readProps[prop] = prop.Name;
                } while (depth < reader.Depth);

                if (reader.NodeType == XmlNodeType.EndElement)
                    reader.Read();
            }

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

            PostDeserialize();
        }