Deserialization of unknown types

C

Chris Dunaway

I need some guidance on how to approach a certain scenario.

I am developing an app that will process text files in various
formats. For each specific format, I will convert the text to xml and
then deserialize into the appropriate object.

For example, suppose I have object types of Order and Invoice. The
text files for each of these is different, but I'll know which type it
is because there will be a header line in each file. I have a class
that will take the text file and an associated .map file and produce
an xml document. The .map file merely contains information about the
structure of the text file and how to construct the xml. This part
is no problem.

However, now that I have the xml, I need to serialize it into
instances of the Order or Invoice classes (or other types). Once I
have done this, the data will then be passed on to other processes for
storage into a database, or other processing.

My problem is in de-serializing the xml. I could use a switch
statement to call the appropriate deserialization routine like this:

string flatFileName = "somefilename.txt";
string objectType= determineObjectType(flatFileName);
string mapFileName = string.Format("{0}.map", objectType);

string xmlString = constructXmlFromFile(flatFileName,
mapFileName); //returns an xml string.
object obj;

switch (objectType) {
case "Order":
obj = deserialize<Order>(xmlString); \\obj is an instance of
Order
break;
case "Invoice":
obj = deserialize<Invoice>(xmlString); \\obj is an instance
of Invoice
break;
default:
throw new Exception("Unknown object type...");
}

This could work, but I might have 15+ different object types and I
would have to edit the switch statement any time I wanted to add a new
type and a switch statement seems a bit unwieldy.

I'd like to come up with a way to do this more generically without
having to maintain a large switch statement, if possible.

Any suggestions would be apprecited.

Thanks,

Chris
 
J

Jeff Johnson

Even if you drop the switch statement, you're going to have to edit your
code every time you introduce a new object type due to the line below:
string objectType= determineObjectType(flatFileName);

Depending on how complex this type determination is, you might be able to
make it data-driven, that is, keep information in a configuration file or a
database and besides the information that identifies what type of file you
have you could also have a column that lists a fully-qualified type name.
This type name would represent your underlying types. All of these types
would implement an interface (we'll call it IMyDeserializable for now) with
one method: DeserializeFromXml(string). Then you could retrieve the type
name, use Activator.CreateInstance() to create a new object of type
IMyDeserializable and then call its DeserializeFromXml() method against the
string you convert. That way you simply need to provide an implementation of
this method for any new object types you introduce.

Simple, huh?
 
F

Family Tree Mike

From what you describe, it seems to me that it would be easier to create the
objects from the text file, then simply use XmlSerialization to create the
xml.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top