skip to Main Content

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


  1. Not very beautiful but what is with:

    if(response.StartsWith("<ResponseType_1>"))
    {
      responseType = typeof(ResponseType_1);
    }
    else if(response.StartsWith("<ResponseType_2>"))
    {
      responseType = typeof(ResponseType_2);
    }
    

    in front of the using?

    Login or Signup to reply.
  2. Then put all the response types into Telegram. Only one property in the response will have a value and the others will be null

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;
    using System.Reflection;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:temptest.xml";
            static void Main(string[] args)
            {
                Telegram telegram = new Telegram();
    
                string type1 = "<Telegram xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:p="LancePlatform">" +
                                    "<Response>" +
                                    //The Rest of the XML Tree
                                    "</Response>" +
                                "</Telegram>";
    
    
                string type2 = "<Telegram xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xmlns:p="LancePlatform">" +
                                    "<Notification>" +
                                    //The Rest of the XML Tree
                                    "</Notification>" +
                                "</Telegram>";
    
                Telegram t1 = telegram.ProcessResponse(type1);
                object[] p1 = Telegram.Properties(t1);
                Telegram t2 = telegram.ProcessResponse(type2);
                object[] p2 = Telegram.Properties(t2);
            }
        }
           public class Telegram
        {
            public Response Response { get; set; }
            public Notification Notification { get; set; }
    
            public static object[] Properties(Telegram t)
            {
                PropertyInfo[] properties = typeof(Telegram).GetProperties();
    
                return properties.Select(x => x.GetValue(t,null)).Where(x => x != null).ToArray();
            }
            public Telegram ProcessResponse(string response)
            {
                Telegram telegram = new Telegram();
                using (TextReader reader = new StringReader(response))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(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);
                    }
                }
                return telegram; 
            }
    
        }
        public class Response
        {
        }
        public class Notification
        {
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search