Following Situation:
I get different XML-formatted responses from a Device. With the response handler class I want to process these responses. Now my problem is how to distinguish these different responses and finding the correct response Type, before deserializing?
The XML Response Types of the Device
- Response Type 1: Response
<Telegram xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns="LancePlatform">
<Response>
// The Rest of the XML Tree
</Response>
</Telegram>
- Response Type 2: Notification
<Telegram xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns="LancePlatform">
<Notification>
// The Rest of the XML Tree
</Notification>
</Telegram>
Process Response Function for One Response Type
public void ProcessResponse(string response)
{
using (TextReader reader = new StringReader(response))
{
XmlSerializer serializer = new XmlSerializer(typeof(Telegram));
Telegram telegram = new Telegram();
try
{
// Do I have to check here if the Deserializion was valid,
// and if not try the next Response Type
telegram = (Telegram)serializer.Deserialize(reader);
Console.WriteLine(telegram.Response);
}
catch (System.Exception ex)
{
Console.WriteLine(ex);
}
}
}
2
Answers
Not very beautiful but what is with:
in front of the using?
Then put all the response types into Telegram. Only one property in the response will have a value and the others will be null