Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Mapping between Schemata while serializing

Question posted by: Joris van Lier (Guest) on June 27th, 2008 07:20 PM
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

 
Not the answer you were looking for? Post your question . . .
190,077 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors