Exemplo n.º 1
0
        /// <exception cref="XmlSerializationException">
        ///   Root element name does not match the name defined by the metadata.
        /// </exception>
        public void ReadRoot(
            Object instance, String customName = null, Boolean ignoreAttributes = false
            )
        {
            if (instance == null)
            {
                throw new ArgumentNullException();
            }
            if (!this.AsAttribute)
            {
                throw new InvalidOperationException();
            }
            Contract.Requires <InvalidOperationException>(
                this.XmlReader.NodeType == XmlNodeType.None || this.XmlReader.NodeType == XmlNodeType.Element
                );

            this.ReadToRoot(customName);

            if (ignoreAttributes)
            {
                while (this.XmlReader.MoveToNextAttribute())
                {
                }
            }
            else
            {
                while (this.XmlReader.MoveToNextAttribute())
                {
                    for (Int32 i = 0; i < this.Serializer.SerializableMembers.Count; i++)
                    {
                        XmlSerializableMember member = this.Serializer.SerializableMembers[i];

                        if (member.MemberInfo.IsAttribute)
                        {
                            Type memberType;
                            if (member.MemberInfo.TryGetMemberTypeByName(this.XmlReader.LocalName, out memberType))
                            {
                                member.SetValue(instance, this.ReadAttributeObject(memberType));
                            }
                        }
                    }
                }
            }
        }