none
Facing issue to convert xml data in to the json and json to class object. RRS feed

  • Question

  • Hi,

    I am getting response content to the API as below

    <?xml version="1.0" encoding="UTF-8" ?>
    <RESPONSE TYPE="PINPRINTING">
       <TERMINALID>ON000009</TERMINALID>
       <LOCALDATETIME>2015-03-04 14:59:00</LOCALDATETIME>
       <SERVERDATETIME>2018-11-16 10:26:51</SERVERDATETIME>
       <TXID>TESTTCG1234567</TXID>
       <HOSTTXID>ON000009000000</HOSTTXID>
       <AMOUNT>30000</AMOUNT>
       <LIMIT>999999999</LIMIT>
       <RESULT>0</RESULT>
       <RESULTTEXT>transaction successful</RESULTTEXT>
       <PINCREDENTIALS>
          <PIN>91555241953167</PIN>
          <SERIAL>1000068265</SERIAL>
          <VALIDTO>3000-01-01 00:00:00</VALIDTO>
       </PINCREDENTIALS>
    </RESPONSE>
    

    Now i want to convert this result into class for accessing the result. Does it possible?

    I am doing as follows but not getting desired result. Please see the code.

    XmlDocument doc = new XmlDocument();
                        doc.LoadXml(res);
                        var json = JsonConvert.SerializeXmlNode(doc);
                        MyClass _myclass = Newtonsoft.Json.JsonConvert.DeserializeObject<MyClass>(json);

    What will be the class structure and how to parse data for xml response?

    Please help me by providing the code.

    Thanks

    Friday, November 16, 2018 10:31 AM

Answers

All replies

  • E.g.

    XmlSerializer serializer = new XmlSerializer(typeof(MyClass));
    using (TextReader reader = new StringReader(res))
    {
        MyClass result = (MyClass) serializer.Deserialize(reader);
    }

    Friday, November 16, 2018 10:40 AM
  • Hi, I am getting below error

    "{"There is an error in XML document (2, 2).""

    What will be the class structure? I am using as below...

     public class ResponseData
        {
            public string TYPE { get; set; }
            public string TERMINALID { get; set; }
            public string LOCALDATETIME { get; set; }
    
            public string SERVERDATETIME { get; set; }
            public string TXID { get; set; }
            public string HOSTTXID { get; set; }
    
            public string AMOUNT { get; set; }
            public string LIMIT { get; set; }
            public string RESULT { get; set; }
            public string RESULTTEXT { get; set; }
        }
        public class MyClass
        {
            public ResponseData resp { get; set; }
            public string TYPE { get; set; }
            public string TERMINALID { get; set; }
            public string LOCALDATETIME { get; set; }
    
            public string SERVERDATETIME { get; set; }
            public string TXID { get; set; }
            public string HOSTTXID { get; set; }
    
            public string AMOUNT { get; set; }
            public string LIMIT { get; set; }
            public string RESULT { get; set; }
            public string RESULTTEXT { get; set; }
            // Fill the missing properties for your data
        }

    Please provide needful help. Thanks.

     

    Friday, November 16, 2018 12:27 PM
  • Do you plan on just reading the data and no updating?
    Friday, November 16, 2018 12:30 PM
  • No, I checked our end. I have no idea what will be the class structure the xml.

    I have plan to store the api response in class or list class for check the response of API.

    <?xml version="1.0" encoding="UTF-8" ?>
    <RESPONSE TYPE="PINPRINTING">
       <TERMINALID>ON000009</TERMINALID>
       <LOCALDATETIME>2015-03-04 14:59:00</LOCALDATETIME>
       <SERVERDATETIME>2018-11-16 12:35:45</SERVERDATETIME>
       <TXID>TESTTCG1234567</TXID>
       <HOSTTXID>ON000009000000</HOSTTXID>
       <AMOUNT>3000</AMOUNT>
       <LIMIT>999999999</LIMIT>
       <RESULT>0</RESULT>
       <RESULTTEXT>transaction successful</RESULTTEXT>
       <PINCREDENTIALS>
          <PIN>61818445717747</PIN>
          <SERIAL>1000060344</SERIAL>
          <VALIDTO>3000-01-01 00:00:00</VALIDTO>
       </PINCREDENTIALS>
    </RESPONSE>

    I did not complete this type of work previously so I am facing issue. Please help me to find desired result. 

    When I am executing the message then getting following error.

    "There is an error in XML document (2, 2)."

    InnerException.Message-<RESPONSE xmlns=''> was not expected.

    Thanks


    Friday, November 16, 2018 12:39 PM
  • Hi got solution by this link

    https://stackoverflow.com/questions/364253/how-to-deserialize-xml-documenta

    Thanks for reply and provide support for all the member.

    • Marked as answer by AnkitKumar2016 Friday, November 16, 2018 1:30 PM
    Friday, November 16, 2018 1:29 PM
  • XML deserialization depends on automapping elements to properties. Thus a REPONSE property must exist in your destination class or the class must be named identically.

    The class structure is much simpler. The only thing is that you need to tell the serializer where to get the TYPE and the credentials list from. Anything else is normally resolved by name.

    namespace ConsoleCS
    {
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Text;
        using System.Xml.Serialization;
    
        public class PinCredentials
        {
            [XmlElement("PIN")] public string Pin { get; set; }
            [XmlElement("SERIAL")] public string Serial { get; set; }
            [XmlElement("VALIDTO")] public string ValidTo { get; set; }
            public override string ToString() { return $"[ Pin: {this.Pin}, Serial: {this.Serial}, ValidTo: {this.ValidTo} ]"; }
        }
    
        public class RESPONSE
        {
            [XmlAttribute] public string TYPE { get; set; }
            public string TERMINALID { get; set; }
            public string LOCALDATETIME { get; set; }
            public string SERVERDATETIME { get; set; }
            public string TXID { get; set; }
            public string HOSTTXID { get; set; }
            public string AMOUNT { get; set; }
            public string LIMIT { get; set; }
            public string RESULT { get; set; }
            public string RESULTTEXT { get; set; }
            [XmlElement] public List<PinCredentials> PINCREDENTIALS { get; set; }
            public override string ToString()
            {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.AppendLine($"Type: {this.TYPE}");
                stringBuilder.AppendLine($"TerminalID: {this.TERMINALID}");
                stringBuilder.AppendLine($"PinCredentials #: {this.PINCREDENTIALS.Count}");
                if (this.PINCREDENTIALS.Count > 0)
                {
                    stringBuilder.AppendLine($"PinCredentials[0]: {this.PINCREDENTIALS[0]}");
                }
    
                return stringBuilder.ToString();
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(RESPONSE));
                using (TextReader reader = new StringReader(GetXmlString()))
                {
                    RESPONSE result = (RESPONSE)serializer.Deserialize(reader);
                    Console.WriteLine(result);
                }
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }
    
            static string GetXmlString()
            {
                return @"
                    <RESPONSE TYPE='PINPRINTING'>
                       <TERMINALID>ON000009</TERMINALID>
                       <LOCALDATETIME>2015-03-04 14:59:00</LOCALDATETIME>
                       <SERVERDATETIME>2018-11-16 10:26:51</SERVERDATETIME>
                       <TXID>TESTTCG1234567</TXID>
                       <HOSTTXID>ON000009000000</HOSTTXID>
                       <AMOUNT>30000</AMOUNT>
                       <LIMIT>999999999</LIMIT>
                       <RESULT>0</RESULT>
                       <RESULTTEXT>transaction successful</RESULTTEXT>
                       <PINCREDENTIALS>
                          <PIN>91555241953167</PIN>
                          <SERIAL>1000068265</SERIAL>
                          <VALIDTO>3000-01-01 00:00:00</VALIDTO>
                       </PINCREDENTIALS>
                    </RESPONSE>
                    ";
            }
        }
    }

    Caveat: the resolving is case-sensitive. This means, when this XML schema is fixed, you're either sticked to ugly names in C', or you need to define each property binding as in shown in the PinCredentials class.

    Friday, November 16, 2018 1:31 PM
  • Post you solution, so that others can learn from it.
    Saturday, November 17, 2018 1:46 PM