对于我使用的二进制序列化
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);
}
我想为 XML 序列化做同样的事情(类实现 IXmlSerializable 接口,因为私有属性设置器),但我不知道如何将对象放入序列化程序(XmlWriter 对象)。
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 ??
}
也许我可以使用这样的东西
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(); // Your xml
但也许有更简单的方法,它只使用我从 WriteXml 方法参数获得的 XmlWriter 对象