I'm trying to serialize/deserialize an object in SOAP-encoded form.
I'm using the code below. Serialization succeeds fine, but when I try
to deserialize I get an exception. Can anyone tell me how I can
deserialize an object which has been serialized this way? Do I need
to do something special with an XmlReader?
(I don't have access to the underlying objects, so SoapFormatter is
not an option for me.)
Thanks,
Ben
public void save(object obj, String filepath) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(obj.Ge tType());
StreamWriter writer = null;
XmlTextWriter xw = null;
try {
writer = new StreamWriter(new FileStream(filepath,
FileMode.Create,
FileAccess.Write,
FileShare.None));
xw = new XmlTextWriter(writer);
XmlSerializer ser = new XmlSerializer(xmlType);
xw.WriteStartDocument();
xw.WriteStartElement("root");
ser.Serialize(xw, obj);
xw.WriteEndElement();
} finally {
if (xw != null) {
xw.Close();
}
if (writer != null) {
writer.Close();
}
}
}
public object load(String filepath, Type type) {
XmlTypeMapping xmlType =
(new SoapReflectionImporter()).ImportTypeMapping(type);
StreamReader reader = null;
try {
XmlSerializer xmlSerializer = new XmlSerializer(xmlType);
reader = new StreamReader(new FileStream(filepath,
FileMode.Open,
FileAccess.Read,
FileShare.Read));
object o = xmlSerializer.Deserialize(reader);
return o;
} finally {
if (reader != null) {
reader.Close();
}
}
}