0

I have an application that is using XML to send and receive information. I will receive XML data like the following:

<?xml version="1.0" encoding="ASCII" ?> 
<Response> 
<ID>1234567</ID> 
<Transaction>Connect</Transaction> 
<Data>data</Data> 
</Response>

Based on the value in the 'Transaction' I field, I plan to use XmlSerializer.Deserialize to deserialize the data packets to the appropriate class type. The example above would be the 'Connect' class that I would have to deserialize to:

using System.Xml.Serialization;

namespace XMLResponse
{
    [XmlRoot("Response")]
    public class Connect
    {
        public Connect()
        {

        }

        public long ID { get; set; }
        public string Transaction { get; set; }
        public string Data { get; set; }
    }
}

I am receiving the data as a byte array right now, I am putting it in a MemoryStream for use with XmlSerializer.Deserialize, but I can put it in a string if needed. What is the best way to pull out that field so I can Deserialize with the right class?

Trevor Vance
  • 321
  • 2
  • 11
  • I you instead made a correct format XML you wouldn't have to deal with this issue. – Franck Jan 04 '22 at 18:43
  • @Franck I'm not sure I understand your comment. The XML is properly formatted. I am not making it either. It is received from another device and I am just processing it. I just need to determine what type of data I am receiving so that I can deserialize it. The example above is only of the specific field I need to process to determine the 'type' of data. – Trevor Vance Jan 04 '22 at 18:46
  • Please provide a [mcve]. `XmlSerializer` has built-in support for polymorphism so if your XML format matches one of the supported formats shown in [Using XmlSerializer to serialize derived classes](https://stackoverflow.com/q/1643139/3744182) you can use that built-in support to deserialize to the correct type. If not, you can pre-load into an `XElement`, search for the required field, then deserialize to the correct type using [Serialize an object to XElement and Deserialize it in memory](https://stackoverflow.com/a/8375101/3744182). – dbc Jan 04 '22 at 18:55
  • Of if the `Connect` element is guaranteed to be at the *beginning* of the XML, it might be more efficient to skip the `XElement` representation and read just the beginning with `XmlReader`. Need to see a [mcve] to be sure. – dbc Jan 04 '22 at 19:06
  • @dbc I have added better examples. Your second comment asks if it will always be the first element. It is not. I thought about just doing a string Search for '' and ''. I just figured there might be something built into C# that would make it cleaner. I will look into your link for 'Using XmlSerializer to serialize derived classes'. Thanks for that! – Trevor Vance Jan 04 '22 at 19:16
  • Are you sure you can't know in advance what format the XML will be? It's unusual to transfer data in a loose contract like that. Prone to bugs also. If this is really really impossible to know, I agree with @dbc that you could load the root into an XElement/XDocument and search for the required field. – Jesse de Wit Jan 04 '22 at 19:57
  • You need to put the string into a stream like StringReader reader = new StringReader(string);. Then put into a xml reader XmlReader xReader = XmlReader.Create(reader); – jdweng Jan 05 '22 at 11:41

0 Answers0