Generise : Deserialize Xml to a specific class of an inherited class

陌路散爱 提交于 2019-12-13 04:45:51

问题


Input pre-processed xml files to map a specific transaction type

Say I have transactionTypeA transactionTypeB transactionTypeC, (all inherited from TransactionTypes). the following code, just for reference, is good for a specific transaction type. for example, mapping xml data into transactionTypeA:

byte[] byteArray = Encoding.UTF8.GetBytes(xmlContent);
MemoryStream tempMemoryStream = new MemoryStream(byteArray);
DataContractSerializer serializer = new DataContractSerializer(typeof(transactionTypeA));
transactionTypeA variavlename= (transactionTypeA)serializer.ReadObject(tempMemoryStream);

Now, I want to make it generic, so that when an xml file is in, I can tell which specific transaction it is.

DataContractSerializer serializer = new DataContractSerializer(typeof(ThatCorrespondingTransactionType));
    ThatCorrespondingTransactionType variavlename= (ThatCorrespondingTransactionType)serializer.ReadObject(tempMemoryStream)

Any help please? I have tried to just use the root parent TransactionTypes, but it gives : SerializationException was unhandled by user code.

Thanks a lot.

来源:https://stackoverflow.com/questions/19009774/generise-deserialize-xml-to-a-specific-class-of-an-inherited-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!