For binary serialization, I use
public ClassConstructor(SerializationInfo info, StreamingContext ctxt) { this.cars = (OtherClass)info.GetValue("Object", typeof(OtherClass)); } public void GetObjectData(SerializationInfo info, StreamingContext ctxt) { info.AddString(this.name); info.AddValue("Object", this.object); }
I want to do the same for XML serialization (the class implements the IXmlSerializable interface, due to the settings of private property seters), but I do not know how to put the object in the serializer (XmlWriter object).
public void WriteXml( XmlWriter writer ) { writer.WriteAttributeString( "Name", Name ); writer. ... Write object, but how ??? } public void ReadXml( XmlReader reader ) { this.Name = reader.GetAttribute( "Name" ); this.object = reader. ... how to read ?? }
Maybe I can use something like this
XmlSerializer xsSubmit = new XmlSerializer(typeof(MyObject)); var subReq = new MyObject(); StringWriter sww = new StringWriter(); XmlWriter writer = XmlWriter.Create(sww); xsSubmit.Serialize(writer, subReq); var xml = sww.ToString();
but maybe there is a simpler method that uses only the XmlWriter object that I get from the WriteXml method argument
source share