Exemplo n.º 1
0
        protected internal override T[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, T[] existingInstance)
        {
            List <T> list = new List <T>();

            this.listHelper.Deserialize(input, format, list);
            return(list.ToArray());
        }
Exemplo n.º 2
0
 public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     Xml.WriteStartElement(format.ElementName);
     Xml.WriteAttributeString("Type", typeSerializer.XmlTypeName);
     typeSerializer.Serialize(this, value, format);
     Xml.WriteEndElement();
 }
Exemplo n.º 3
0
        internal void ReadSharedResources()
        {
            if (!MoveToElement("Resources"))
            {
                return;
            }

            var resources      = new Dictionary <string, object>();
            var resourceFormat = new ContentSerializerAttribute {
                ElementName = "Resource"
            };

            // Read all the resources.
            Xml.ReadStartElement();
            while (MoveToElement("Resource"))
            {
                var id       = Xml.GetAttribute("ID");
                var resource = ReadObject <object>(resourceFormat);
                resources.Add(id, resource);
            }
            Xml.ReadEndElement();

            // Execute the fixups.
            foreach (var fixup in _resourceFixups)
            {
                object resource;
                if (!resources.TryGetValue(fixup.Key, out resource))
                {
                    throw new InvalidContentException("Missing shared resource \"" + fixup.Key + "\".");
                }
                fixup.Value(resource);
            }
        }
Exemplo n.º 4
0
 /// <summary>Writes a single object to the output XML stream as an instance of the specified type.</summary>
 /// <param name="value">The value to write.</param>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 public void WriteRawObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     if (value == null)
     {
         throw new ArgumentException("value");
     }
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (typeSerializer == null)
     {
         throw new ArgumentNullException("typeSerializer");
     }
     if (!format.FlattenContent)
     {
         if (string.IsNullOrEmpty(format.ElementName))
         {
             throw new ArgumentException(Resources.NullElementName);
         }
         this.Xml.WriteStartElement(format.ElementName);
     }
     typeSerializer.Serialize(this, value, format);
     if (!format.FlattenContent)
     {
         this.Xml.WriteEndElement();
     }
 }
Exemplo n.º 5
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;

            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }
            }

            // First deserialize the base type.
            if (_baseSerializer != null)
            {
                _baseSerializer.Deserialize(input, format, result);
            }

            // Now deserialize our own elements.
            foreach (var info in _elements)
            {
                if (!info.Attribute.FlattenContent)
                {
                    if (!input.MoveToElement(info.Attribute.ElementName))
                    {
                        // If the the element was optional then we can
                        // safely skip it and continue.
                        if (info.Attribute.Optional)
                        {
                            continue;
                        }

                        // We failed to find a required element.
                        throw new InvalidContentException(string.Format("The Xml element `{0}` is required!", info.Attribute.ElementName));
                    }
                }

                if (info.Attribute.SharedResource)
                {
                    Action <object> fixup = (o) => info.Setter(result, o);
                    input.ReadSharedResource(info.Attribute, fixup);
                }
                else if (info.Setter == null)
                {
                    var value = info.Getter(result);
                    input.ReadObject(info.Attribute, info.Serializer, value);
                }
                else
                {
                    var value = input.ReadObject <object>(info.Attribute, info.Serializer);
                    info.Setter(result, value);
                }
            }

            return(result);
        }
Exemplo n.º 6
0
        public T ReadRawObject <T>(ContentSerializerAttribute format, ContentTypeSerializer typeSerializer, T existingInstance)
        {
            if (format.FlattenContent)
            {
                Xml.MoveToContent();
                return((T)typeSerializer.Deserialize(this, format, existingInstance));
            }

            if (!MoveToElement(format.ElementName))
            {
                throw NewInvalidContentException(null, "Element '{0}' was not found.", format.ElementName);
            }

            var isEmpty = Xml.IsEmptyElement;

            if (!isEmpty)
            {
                Xml.ReadStartElement();
            }

            var result = typeSerializer.Deserialize(this, format, existingInstance);

            if (isEmpty)
            {
                Xml.Skip();
            }

            if (!isEmpty)
            {
                Xml.ReadEndElement();
            }

            return((T)result);
        }
 public void Setup()
 {
     attribute                = new ContentSerializerAttribute();
     attribute.AllowNull      = true;
     attribute.ElementName    = "Name";
     attribute.FlattenContent = true;
 }
Exemplo n.º 8
0
        public void ReadSharedResource <T>(ContentSerializerAttribute format, Action <T> fixup)
        {
            string str;

            if (format.FlattenContent)
            {
                str = Xml.ReadContentAsString();
            }
            else
            {
                if (!MoveToElement(format.ElementName))
                {
                    throw NewInvalidContentException(null, "Element '{0}' was not found.", format.ElementName);
                }

                str = Xml.ReadElementContentAsString();
            }

            // Do we already have one for this?
            Action <object> prevFixup;

            if (!_resourceFixups.TryGetValue(str, out prevFixup))
            {
                _resourceFixups.Add(str, (o) => fixup((T)o));
            }
            else
            {
                _resourceFixups[str] = (o) =>
                {
                    prevFixup(o);
                    fixup((T)o);
                };
            }
        }
Exemplo n.º 9
0
        public void Deserialize(IntermediateReader input, ContentSerializerAttribute format, object collection)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            this.ValidateCollectionType(collection);
            IXmlListItemSerializer xmlListItemSerializer = this.contentSerializer as IXmlListItemSerializer;

            if (xmlListItemSerializer != null)
            {
                XmlListReader xmlListReader = new XmlListReader(input);
                while (!xmlListReader.AtEnd)
                {
                    this.addToCollection(collection, xmlListItemSerializer.Deserialize(xmlListReader));
                }
                return;
            }
            ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute();

            contentSerializerAttribute.ElementName = format.CollectionItemName;
            while (input.MoveToElement(format.CollectionItemName))
            {
                this.addToCollection(collection, input.ReadObject <object>(contentSerializerAttribute, this.contentSerializer));
            }
        }
Exemplo n.º 10
0
 /// <summary>Writes a single object to the output XML stream, using the specified type hint.</summary>
 /// <param name="value">The value to write.</param>
 /// <param name="format">The format of the XML.</param>
 /// <param name="typeSerializer">The type serializer.</param>
 public void WriteObject <T>(T value, ContentSerializerAttribute format, ContentTypeSerializer typeSerializer)
 {
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (typeSerializer == null)
     {
         throw new ArgumentNullException("typeSerializer");
     }
     if (!format.FlattenContent)
     {
         if (string.IsNullOrEmpty(format.ElementName))
         {
             throw new ArgumentException(Resources.NullElementName);
         }
         this.Xml.WriteStartElement(format.ElementName);
     }
     if (value == null)
     {
         if (format.FlattenContent)
         {
             throw new InvalidOperationException(Resources.CantWriteNullInFlattenContentMode);
         }
         this.Xml.WriteAttributeString("Null", "true");
     }
     else
     {
         Type type = value.GetType();
         if (type.IsSubclassOf(typeof(Type)))
         {
             type = typeof(Type);
         }
         if (type != typeSerializer.BoxedTargetType)
         {
             if (format.FlattenContent)
             {
                 throw new InvalidOperationException(Resources.CantWriteDynamicTypesInFlattenContentMode);
             }
             typeSerializer = this.Serializer.GetTypeSerializer(type);
             this.Xml.WriteStartAttribute("Type");
             this.WriteTypeName(typeSerializer.TargetType);
             this.Xml.WriteEndAttribute();
         }
         if (this.recurseDetector.ContainsKey(value))
         {
             throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resources.FoundCyclicReference, new object[]
             {
                 value
             }));
         }
         this.recurseDetector.Add(value, true);
         typeSerializer.Serialize(this, value, format);
         this.recurseDetector.Remove(value);
     }
     if (!format.FlattenContent)
     {
         this.Xml.WriteEndElement();
     }
 }
 public NonGenericIDictionarySerializer(Type targetType) : base(targetType)
 {
     this.keyFormat               = new ContentSerializerAttribute();
     this.valueFormat             = new ContentSerializerAttribute();
     this.keyFormat.ElementName   = "Key";
     this.valueFormat.ElementName = "Value";
 }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            IDictionary dictionary;

            if (existingInstance == null)
            {
                dictionary = (IDictionary)Activator.CreateInstance(base.TargetType);
            }
            else
            {
                dictionary = this.CastType(existingInstance);
            }
            while (input.MoveToElement(format.CollectionItemName))
            {
                input.Xml.ReadStartElement();
                object key   = input.ReadObject <object>(this.keyFormat);
                object value = input.ReadObject <object>(this.valueFormat);
                dictionary.Add(key, value);
                input.Xml.ReadEndElement();
            }
            return(dictionary);
        }
Exemplo n.º 13
0
        protected internal override List <T> Deserialize(IntermediateReader input, ContentSerializerAttribute format, List <T> existingInstance)
        {
            var result = existingInstance ?? new List <T>();

            var elementSerializer = _itemSerializer as ElementSerializer <T>;

            if (elementSerializer != null)
            {
                elementSerializer.Deserialize(input, result);
            }
            else
            {
                // Create the item serializer attribute.
                var itemFormat = new ContentSerializerAttribute();
                itemFormat.ElementName = format.CollectionItemName;

                // Read all the items.
                while (input.MoveToElement(itemFormat.ElementName))
                {
                    var value = input.ReadObject <T>(itemFormat, _itemSerializer);
                    result.Add(value);
                }
            }

            return(result);
        }
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }
            if (format == null)
            {
                throw new ArgumentNullException("format");
            }
            IList list;

            if (existingInstance == null)
            {
                list = (IList)Activator.CreateInstance(base.TargetType);
            }
            else
            {
                list = this.CastType(existingInstance);
            }
            ContentSerializerAttribute contentSerializerAttribute = new ContentSerializerAttribute();

            contentSerializerAttribute.ElementName = format.CollectionItemName;
            while (input.MoveToElement(format.CollectionItemName))
            {
                list.Add(input.ReadObject <object>(contentSerializerAttribute));
            }
            return(list);
        }
Exemplo n.º 15
0
        public static T Deserialize <T>(XmlReader input, string referenceRelocationPath)
        {
            var serializer = new IntermediateSerializer();
            var reader     = new IntermediateReader(serializer, input, referenceRelocationPath);

            if (!reader.MoveToElement("XnaContent"))
            {
                throw new InvalidContentException(string.Format("Could not find XnaContent element in '{0}'.", referenceRelocationPath));
            }

            // Initialize the namespace lookups from
            // the attributes on the XnaContent element.
            serializer.CreateNamespaceLookup(input);

            // Move past the XnaContent.
            input.ReadStartElement();

            // Read the asset.
            var format = new ContentSerializerAttribute {
                ElementName = "Asset"
            };
            var asset = reader.ReadObject <T>(format);

            // TODO: Read the shared resources and external
            // references here!

            // Move past the closing XnaContent element.
            input.ReadEndElement();

            return(asset);
        }
Exemplo n.º 16
0
        protected override void Serialize(IntermediateWriter output,
                                          SharedResourceDictionary <TKey, TValue> value,
                                          ContentSerializerAttribute format)
        {
            foreach (TKey key in value.Keys)
            {
                if (default(TKey) is ValueType)
                {
                    output.WriteObject(key, Keyformat);
                }
                else
                {
                    output.WriteSharedResource(key, Keyformat);
                }

                if (default(TValue) is ValueType)
                {
                    output.WriteObject(value[key], Valueformat);
                }
                else
                {
                    output.WriteSharedResource(value[key], Valueformat);
                }
            }
        }
Exemplo n.º 17
0
 /// <summary>
 /// Deserializes the derived fields.
 /// </summary>
 /// <param name="input">The input.</param>
 /// <param name="format">The format.</param>
 /// <param name="existingInstance">The existing instance.</param>
 protected override void DeserializeDerivedFields(IntermediateReader input, ContentSerializerAttribute format, BoxEmitter existingInstance)
 {
     existingInstance.Width    = input.ReadObject <Single>("Width");
     existingInstance.Height   = input.ReadObject <Single>("Height");
     existingInstance.Depth    = input.ReadObject <Single>("Depth");
     existingInstance.Rotation = input.ReadObject <Vector3>("Rotation");
 }
Exemplo n.º 18
0
 /// <summary>Adds a shared reference to the output XML and records the object to be serialized later.</summary>
 /// <param name="value">The value to write.</param>
 /// <param name="format">The format of the XML.</param>
 public void WriteSharedResource <T>(T value, ContentSerializerAttribute format)
 {
     if (format == null)
     {
         throw new ArgumentNullException("format");
     }
     if (!format.FlattenContent)
     {
         if (string.IsNullOrEmpty(format.ElementName))
         {
             throw new ArgumentException(Resources.NullElementName);
         }
         this.Xml.WriteStartElement(format.ElementName);
     }
     if (value != null)
     {
         string text;
         if (!this.sharedResourceNames.TryGetValue(value, out text))
         {
             text = "#Resource" + (this.sharedResourceNames.Count + 1).ToString(CultureInfo.InvariantCulture);
             this.sharedResourceNames.Add(value, text);
             this.sharedResources.Enqueue(value);
         }
         this.Xml.WriteString(text);
     }
     if (!format.FlattenContent)
     {
         this.Xml.WriteEndElement();
     }
 }
Exemplo n.º 19
0
 protected override LayerMask Deserialize(IntermediateReader input, ContentSerializerAttribute format, LayerMask existingInstance)
 {
     return(new LayerMask()
     {
         value = input.Xml.ReadContentAsInt()
     });
 }
Exemplo n.º 20
0
        protected internal override T?Deserialize(IntermediateReader input, ContentSerializerAttribute format, T?existingInstance)
        {
            var list = new List <T>();

            _serializer.Deserialize(input, list);
            return(list.First());
        }
Exemplo n.º 21
0
        internal void ReadSharedResources()
        {
            if (!MoveToElement("Resources"))
            {
                return;
            }

            var resources      = new Dictionary <string, object>();
            var resourceFormat = new ContentSerializerAttribute {
                ElementName = "Resource"
            };

            // Read all the resources.
            Xml.ReadStartElement();
            while (MoveToElement("Resource"))
            {
                var id       = Xml.GetAttribute("ID");
                var resource = ReadObject <object>(resourceFormat);
                resources.Add(id, resource);
            }
            Xml.ReadEndElement();

            // Execute the fixups.
            foreach (var fixup in _resourceFixups)
            {
                var resouce = resources[fixup.Key];
                fixup.Value(resouce);
            }
        }
Exemplo n.º 22
0
        protected override Vector4[] Deserialize(IntermediateReader input, ContentSerializerAttribute format, Vector4[] existingInstance)
        {
            string str = input.Xml.ReadContentAsString().Trim();

            if (string.IsNullOrWhiteSpace(str))
            {
                return(new Vector4[0]);
            }

            string[] s = str.Split(' ');
            if ((s.Length % 4) != 0)
            {
                throw new Exception(String.Format("Not enough floats in the string for Vector4[] in element {0}. Was {1}.", format.ElementName, s.Length));
            }
            Vector4[] vs = new Vector4[s.Length / 4];
            for (int i = 0; i < vs.Length; i++)
            {
                try
                {
                    int triIndex = i * 4;
                    vs[i][0] = float.Parse(s[triIndex]);
                    vs[i][1] = float.Parse(s[triIndex + 1]);
                    vs[i][2] = float.Parse(s[triIndex + 2]);
                }
                catch (IndexOutOfRangeException)
                {
                    throw new IndexOutOfRangeException("Error reading Vector4[] from string in element " + format.ElementName + ". i was " + i + " for a limit of " + vs.Length + " using " + s.Length + " numbers");
                }
            }
            return(vs);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Deserializes a $safeitemname$ object from intermediate XML format.
        /// </summary>
        /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param>
        /// <param name="format">Specifies the intermediate source XML format.</param>
        /// <param name="existingInstance">The strongly typed object containing the received data, or null if the
        /// deserializer should construct a new instance.</param>
        /// <returns>A deserialized $safeitemname$ instance.</returns>
        protected override $safeitemname$ Deserialize(IntermediateReader input, ContentSerializerAttribute format, $safeitemname$ existingInstance)
        {
            $safeitemname$ value = existingInstance ?? default($safeitemname$);

            // TODO deserialize $safeitemname$ fields & properties...

            return(value);
        }
Exemplo n.º 24
0
 protected internal override Type Deserialize(IntermediateReader input, ContentSerializerAttribute format, Type existingInstance)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input");
     }
     return(input.ReadTypeName());
 }
Exemplo n.º 25
0
        /// <summary>
        /// Deserializes a CooldownController object from intermediate XML format.
        /// </summary>
        /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param>
        /// <param name="format">Specifies the intermediate source XML format.</param>
        /// <param name="existingInstance">The strongly typed object containing the received data, or null if the
        /// deserializer should construct a new instance.</param>
        /// <returns>A deserialized CooldownController instance.</returns>
        protected override CooldownController Deserialize(IntermediateReader input, ContentSerializerAttribute format, CooldownController existingInstance)
        {
            CooldownController value = existingInstance ?? new CooldownController();

            value.CooldownPeriod = input.ReadObject <Single>("CooldownPeriod");

            return(value);
        }
Exemplo n.º 26
0
 protected internal override DateTime Deserialize(IntermediateReader input, ContentSerializerAttribute format, DateTime existingInstance)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input");
     }
     return(XmlConvert.ToDateTime(input.Xml.ReadContentAsString(), XmlDateTimeSerializationMode.RoundtripKind));
 }
Exemplo n.º 27
0
 protected internal override TimeSpan Deserialize(IntermediateReader input, ContentSerializerAttribute format, TimeSpan existingInstance)
 {
     if (input == null)
     {
         throw new ArgumentNullException("input");
     }
     return(XmlConvert.ToTimeSpan(input.Xml.ReadContentAsString()));
 }
Exemplo n.º 28
0
        /// <summary>
        /// Deserializes a RotationModifier object from intermediate XML format.
        /// </summary>
        /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param>
        /// <param name="format">Specifies the intermediate source XML format.</param>
        /// <param name="existingInstance">The strongly typed object containing the received data, or null if the
        /// deserializer should construct a new instance.</param>
        /// <returns>A deserialized RotationModifier instance.</returns>
        protected override RotationModifier Deserialize(IntermediateReader input, ContentSerializerAttribute format, RotationModifier existingInstance)
        {
            RotationModifier value = existingInstance ?? new RotationModifier();

            value.RotationRate = input.ReadObject <Vector3>("RotationRate");

            return(value);
        }
Exemplo n.º 29
0
 protected internal override void Initialize(IntermediateSerializer serializer)
 {
     _serializer = serializer.GetTypeSerializer(typeof(T));
     _format     = new ContentSerializerAttribute
     {
         FlattenContent = true
     };
 }
Exemplo n.º 30
0
        /// <summary>
        /// Deserializes a HueShiftModifier object from intermediate XML format.
        /// </summary>
        /// <param name="input">Location of the intermediate XML and various deserialization helpers.</param>
        /// <param name="format">Specifies the intermediate source XML format.</param>
        /// <param name="existingInstance">The strongly typed object containing the received data, or null if the
        /// deserializer should construct a new instance.</param>
        /// <returns>A deserialized HueShiftModifier instance.</returns>
        protected override HueShiftModifier Deserialize(IntermediateReader input, ContentSerializerAttribute format, HueShiftModifier existingInstance)
        {
            HueShiftModifier value = existingInstance ?? new HueShiftModifier();

            value.HueShift = input.ReadObject <Single>("HueShift");

            return(value);
        }
Exemplo n.º 31
0
        protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format)
        {
            // Create the item serializer attribute.
            var itemFormat = new ContentSerializerAttribute();
            itemFormat.ElementName = format.CollectionItemName;

            // Read all the items.
            foreach (var item in (IList) value)
                output.WriteObject(item, itemFormat);
        }
Exemplo n.º 32
0
 protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
 {
     var str = input.Xml.ReadString();
     try
     {
         return Enum.Parse(TargetType, str, true);
     }
     catch (Exception ex)
     {
         throw input.NewInvalidContentException(ex, "Invalid enum value '{0}' for type '{1}'", str, TargetType.Name);
     }
 }
Exemplo n.º 33
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = (IList) (existingInstance ?? Activator.CreateInstance(TargetType));

            // Create the item serializer attribute.
            var itemFormat = new ContentSerializerAttribute();
            itemFormat.ElementName = format.CollectionItemName;

            // Read all the items.
            while (input.MoveToElement(itemFormat.ElementName))
            {
                var value = input.ReadObject<object>(itemFormat);
                result.Add(value);
            }

            return result;
        }
Exemplo n.º 34
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;
            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }                
            }

            // First deserialize the base type.
            if (_baseSerializer != null)
                _baseSerializer.Deserialize(input, format, result);

            // Now deserialize our own elements.
            foreach (var info in _elements)
            {
                if (!info.Attribute.FlattenContent)
                {
                    if (!input.MoveToElement(info.Attribute.ElementName))
                    {
                        // If the the element was optional then we can
                        // safely skip it and continue.
                        if (info.Attribute.Optional)
                            continue;
                        
                        // We failed to find a required element.
                        throw new InvalidContentException(string.Format("The Xml element `{0}` is required!", info.Attribute.ElementName));
                    }
                }

                if (info.Attribute.SharedResource)
                {
                    Action<object> fixup = (o) => info.Setter(result, o);
                    input.ReadSharedResource(info.Attribute, fixup);
                }
                else if (info.Setter == null)
                {
                    var value = info.Getter(result);
                    input.ReadObject(info.Attribute, info.Serializer, value);
                }
                else
                {
                    var value = input.ReadObject<object>(info.Attribute, info.Serializer);
                    info.Setter(result, value);
                }
            }

            return result;
        }
Exemplo n.º 35
0
        protected internal override object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
        {
            var result = existingInstance;
            if (result == null)
            {
                try
                {
                    result = Activator.CreateInstance(TargetType, true);
                }
                catch (MissingMethodException e)
                {
                    throw new Exception(string.Format("Couldn't create object of type {0}: {1}", TargetType.Name, e.Message), e);
                }                
            }

            var reader = input.Xml;
            var depth = reader.Depth;

            // Read the next node.
            while (reader.Read())
            {
                // Did we reach the end of this object?
                if (reader.NodeType == XmlNodeType.EndElement)
                    break;

                Debug.Assert(reader.Depth == depth, "We are not at the right depth!");

                if (reader.NodeType == XmlNodeType.Element)
                {
                    var elementName = reader.Name;
                    reader.ReadStartElement();

                    ElementInfo info;
                    if (!_elements.TryGetValue(elementName, out info))
                        throw new InvalidContentException(string.Format("Element `{0}` was not found in type `{1}`.", elementName, TargetType));
                    var value = info.Serializer.Deserialize(input, format, null);
                    info.Setter(result, value);
                    reader.ReadEndElement();
                    continue;
                }

                // If we got here then we were not interested 
                // in this node... so skip its children.
                reader.Skip();
            }

            return result;
        }
		protected internal override object Deserialize (IntermediateReader input, ContentSerializerAttribute format, object existingInstance)
		{
			throw new System.NotImplementedException();
		}
		protected internal override void Serialize (IntermediateWriter output, object value, ContentSerializerAttribute format)
		{
			throw new System.NotImplementedException();
		}
Exemplo n.º 38
0
 protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format)
 {
     Debug.Assert(value.GetType() == TargetType, "Got invalid value type!");
     output.Xml.WriteString(value.ToString());
 }
Exemplo n.º 39
0
 protected internal abstract object Deserialize(IntermediateReader input, ContentSerializerAttribute format, object existingInstance);
Exemplo n.º 40
0
 protected internal abstract void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format);
Exemplo n.º 41
0
        protected internal override void Serialize(IntermediateWriter output, object value, ContentSerializerAttribute format)
        {
            // First serialize the base type.
            if (_baseSerializer != null)
                _baseSerializer.Serialize(output, value, format);

            // Now serialize our own elements.
            foreach (var info in _elements)
            {
                var elementValue = info.Getter(value);

                if (info.Attribute.SharedResource)
                    output.WriteSharedResource(elementValue, info.Attribute);
                else
                    output.WriteObjectInternal(elementValue, info.Attribute, info.Serializer, info.Serializer.TargetType);
            }
        }