Hi,
I'm looking for advice on mapping between different XML Schamata.
The C# (2.0) project I'm currently working on needs to send custom business
entities to multiple webservices, each accepting the same entity but in a
different schema.
As an extra complication the local entities are not serializable, so I have
implemented ISerializationSurrogate to fill the StreamingContext with object
data.
I'm looking for a way to map the name/value pairs in StreamingContext to the
target schema.
interface Local.IEntity { DateTime startDate; DateTime endDate;}
interface Remote.IEntity { DateTime[] days; }
What I have written so-far (see below) feels like I'm overcomplicating
things, please suggest a better way.
sealed class Local2RemoteSerializationBinder : SerializationBinder
{
public override Type BindToType(string assemblyName, string
typeName)
{
switch (typeName)
{
case "Local.Entity":
return typeof(Remote.Entity);
}
}
}
class Converter : ISerializationSurrogate {
Remote.IEntity Convert(Local.IEntity input)
{
SurrogateSelector ss = new SurrogateSelector();
ss.AddSurrogate(this);
StreamingContext sc = new
StreamingContext(StreamingContextStates.Remoting);
ISerializationSurrogate surrogate = new Converter();
IFormatter formatter = new BinaryFormatter(ss); // could be an
XML formatter
MemoryStream ms = new MemoryStream();
formatter.Serialize(ms, input);
// reset the memorystream
ms.Position = 0;
formatter.Binder = new Local2RemoteSerializationBinder();
return (Remote.IEntity)formatter.Deserialize(ms);
}
#region ISerializationSurrogate Members
//... removed for brevity
#endregion
}
--
Joris van Lier