Ejemplo n.º 1
0
        /// <summary>
        /// Overridden. Causes the system to throw an exception.
        /// </summary>
        /// <param name="elementName">The name of the unrecognized element.</param>
        /// <param name="reader">An input stream that reads XML from the xml file.</param>
        /// <returns>true if the unrecognized element was deserialized successfully; otherwise, false. The default is false.</returns>
        protected override bool OnDeserializeUnrecognizedElement(string elementName, XmlReader reader)
        {
            if (elementName == _addElementName)
            {
                APXmlElement elem = CreateNewElementInternal(null);
                elem.DeserializeElement(reader, false);
                BaseAdd(elem);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Reads XML from the xml file.
        /// </summary>
        /// <param name="reader">The XmlReader that reads from the xml 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())
            {
                APXmlPropertyInformation 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 APXmlException(APResource.GetString(APResource.APXml_UnrecognizedAttribute, reader.LocalName));
                    }

                    continue;
                }

                if (readProps.ContainsKey(prop))
                {
                    throw new APXmlException(APResource.GetString(APResource.APXml_DuplicateAttribute, prop.Name));
                }

                string value = null;
                try
                {
                    value = reader.Value;
                    ValidateValue(prop.Property, value);
                    prop.SetStringValue(value);
                }
                catch (APXmlException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    throw new APXmlException(APResource.GetString(APResource.APXml_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;
                    }

                    APXmlPropertyInformation prop = ElementInformation.Properties[reader.LocalName];
                    if (prop == null || (serializeCollectionKey && !prop.IsKey))
                    {
                        if (!OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                        {
                            if (prop == null)
                            {
                                APXmlElementCollection collection = GetDefaultCollection();
                                if (collection != null && collection.OnDeserializeUnrecognizedElement(reader.LocalName, reader))
                                {
                                    continue;
                                }
                            }
                            throw new APXmlException(APResource.GetString(APResource.APXml_UnrecognizedElement, reader.LocalName));
                        }
                        continue;
                    }

                    if (!prop.IsElement)
                    {
                        throw new APXmlException(APResource.GetString(APResource.APXml_NotAElement, prop.Name));
                    }

                    if (readProps.Contains(prop))
                    {
                        throw new APXmlException(APResource.GetString(APResource.APXml_DuplicateElement, prop.Name));
                    }

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

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

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

            PostDeserialize();
        }