PC Review


Reply
Thread Tools Rate Thread

Deserialization of unknown types

 
 
Chris Dunaway
Guest
Posts: n/a
 
      24th Oct 2008
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
 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      24th Oct 2008
"Chris Dunaway" <(E-Mail Removed)> wrote in message
news:a3583dda-06cf-46af-ac96-(E-Mail Removed)...

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?


 
Reply With Quote
 
Family Tree Mike
Guest
Posts: n/a
 
      24th Oct 2008
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.

"Chris Dunaway" <(E-Mail Removed)> wrote in message
news:a3583dda-06cf-46af-ac96-(E-Mail Removed)...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unknown file types =?Utf-8?B?VG9t?= Microsoft Frontpage 1 27th Jul 2007 05:27 PM
unknown file types =?Utf-8?B?U3Vuc2hpbmU=?= Windows XP Help 1 21st Nov 2006 05:33 PM
deserialization of xml with unknown object type? KenK Microsoft C# .NET 5 10th Jan 2006 04:55 AM
Unknown mime types Randy Harris Windows XP General 0 24th Mar 2004 02:21 AM
Help, DataSet serialization and deserialization hides the original column types Dimitar Dimtirov Microsoft ADO .NET 0 14th Jul 2003 02:49 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:38 PM.