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
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