/// <summary> /// Writes the serialized object to the XML stream. /// </summary> /// <param name="output">A pre-initialized <see cref="XmlTextWriter"/> object.</param> /// <param name="obj">Object to serialize.</param> public void WriteObject(XmlWriter output, object obj) { IDictionary thisDict = obj as IDictionary; if (thisDict == null) throw new WddxException("Attemped to serialize incompatible object. Expected: " + typeof(IDictionary).FullName + " but got: " + ((obj == null) ? "null" : obj.GetType().FullName)); WddxObjectSerializerFactory factory = new WddxObjectSerializerFactory(); output.WriteStartElement("struct"); object thisObject; foreach (object Key in thisDict.Keys) { output.WriteStartElement("var"); output.WriteAttributeString("name", Key.ToString()); thisObject = thisDict[Key]; factory.GetSerializer(thisObject).WriteObject(output, thisObject); output.WriteEndElement(); } output.WriteEndElement(); }
/// <summary> /// Serializes an object to a pre-existing <see cref="XmlWriter"/> object. /// </summary> /// <remarks> /// The <see cref="XmlWriter"/> should be pre-initialized, and can be pointed at many things: /// a string, a file, a network stream, the ASP.NET Response stream, etc. /// </remarks> /// <param name="output">A pre-initialized <see cref="XmlWriter"/> object.</param> /// <param name="obj">The object to be serialized.</param> public void Serialize(XmlWriter output, object obj) { WritePacketHeader(output); // write packet contents WddxObjectSerializerFactory factory = new WddxObjectSerializerFactory(); IWddxObjectSerializer serializer = factory.GetSerializer(obj); serializer.WriteObject(output, obj); WritePacketFooter(output); }
/// <summary> /// Writes the serialized object to the XML stream. /// </summary> /// <param name="output">A pre-initialized <see cref="XmlTextWriter"/> object.</param> /// <param name="obj">Object to serialize.</param> public void WriteObject(XmlWriter output, object obj) { WddxObjectSerializerFactory factory = new WddxObjectSerializerFactory(); ICollection thisCollection = obj as ICollection; if (thisCollection == null) throw new WddxException("Attemped to serialize incompatible object. Expected: " + typeof(ICollection).FullName + " but got: " + ((obj == null) ? "null" : obj.GetType().FullName)); output.WriteStartElement("array"); output.WriteAttributeString("length", thisCollection.Count.ToString()); foreach (object thisObject in thisCollection) { factory.GetSerializer(thisObject).WriteObject(output, thisObject); } output.WriteEndElement(); }