Exemplo n.º 1
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();
     }
 }
Exemplo n.º 2
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();
     }
 }