none
null value during deserilzing xml string to object RRS feed

  • Question

  • hi all,

    I m getting null value for transacition-number in my xml response while deserliazing xmlstring to object in C#.

    xml post response should be:-

    <request type="9" terminal-id="1" login="A9ABC577-94D6-48EC-BB7C-00C525CA55F9">
        <terminal-id>1</terminal-id>
        <transaction-number>4204579018</transaction-number>
        <to>
            <service-id>100000</service-id>
            <account-number>550550556</account-number>
            <amount>10</amount>
        </to>
    </request>

    Class model file

    [XmlRoot("request")]
        public  class DecryptedTerminalData
        {
            [XmlAttribute("type")]
            public int Type { get; set; }

            [XmlAttribute("terminal-id")]
            public int Id { get; set; }

            [XmlAttribute("login")]
            public string Login { get; set; }

            [XmlElement("terminal-id", Type = typeof(TerminalId))]
            public TerminalId[] TerminalIDs { get; set; }

            [XmlElement("transaction-number", Type = typeof(TransactionNumber))]
            public TransactionNumber[] TransactionNumbers { get; set; }
            
            
            [XmlElement("to", Type = typeof(To))]
            public To[] Tos { get; set; }

            
           
         public DecryptedTerminalData()
         {
              TerminalIDs = null;
              TransactionNumbers = null;
              Tos = null;
         }
           
            
       }

        [Serializable]
        [XmlRoot("terminal-id")]
        public  class TerminalId
        {
            public int Id;
            //{ get; set; }
            public TerminalId()
            {

            }
        }

        [Serializable]
        [XmlRoot("transaction-number")]
        public  class TransactionNumber
        {
           // private XElement root = XElement.Parse(xml);
            public int TransNumber;


            public TransactionNumber()
            {
                
            }
        }

        [Serializable]
        [XmlRoot("to")]
        public class To
        {

            [XmlElement("service-id")]
            public int ServiceId;
            //{ get; set; }

            [XmlElement("account-number")]
            public int Accountnumber;
            //{ get; set; }

            [XmlElement("amount")]
            public int Amount;
            //{ get; set; }
            public To()
            {
            }
        }

    and Service file

     public class Service
        {
            // ReSharper disable UnusedMember.Global
            [WebInvoke(Method = "POST", UriTemplate = "")]
            public DecryptedTerminalData GetEncryptedData(Stream data)
            {
               var streamReader = new StreamReader(data);

                var encodedData = streamReader.ReadToEnd();
     
                var terminalData = Serialization.DeserializeFromString<TerminalData>(encodedData);

                var code = Sql.CheckLogin(terminalData);

                var decryptedData = RC4.RC4Decrypt(StringToByteArray(code), terminalData.Data);

                if (terminalData.CompressionType == "gzip")
                    decryptedData = Gzip.GzipDecompress(decryptedData);

                var xml = Encoding.ASCII.GetString(decryptedData);

                var decryptData = Serialization.DeserializeFromString<DecryptedTerminalData>(xml);

              //  var xDocument = XDocument.Parse(decryptData);

                return decryptData;
            }

    Your answers are valuable!

    Thursday, June 20, 2013 2:55 PM

Answers

  • I find a similar thread about your question,  the thread said you should change XMLAttribute to XMLElement, for more information, you can refer to the link: http://stackoverflow.com/questions/4252225/why-deserialize-xml-into-object-return-null-value


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample


    • Edited by Learning hard Friday, June 21, 2013 3:39 AM update
    • Marked as answer by maddyboss Friday, May 13, 2016 2:42 PM
    Friday, June 21, 2013 3:34 AM
  • The following results in the output below:

    <?xml version="1.0" encoding="utf-8"?>
    <request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="9" te
    rminal-id="1" login="A9ABC577-94D6-48EC-BB7C-00C525CA55F9">
      <terminal-id>
        <Id>1</Id>
      </terminal-id>
      <transaction-number>
        <TransNumber>4204579</TransNumber>
      </transaction-number>
      <to>
        <service-id>100000</service-id>
        <account-number>550550556</account-number>
        <amount>10</amount>
      </to>
    </request>
    
    

    It's not exactly what you have in mind, but not far from that. Note that the type int is not bit enough for a transaction number like that in your example above (4204579018), for such a big number it needs to be uint.

    Here is my code, it should give you a direction at least:

    using System;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace XmlSerializing_1 {
    	class Program {
    		static string _PATH = Path.Combine(Path.GetTempPath(), "Test\\terminalData.xml");
    		static void Main(string[] args) {
    			DecryptedTerminalData decryptedData = new DecryptedTerminalData();
    			decryptedData.Id = 1;
    			decryptedData.Login = "A9ABC577-94D6-48EC-BB7C-00C525CA55F9";
    			decryptedData.Type = 9;
    			decryptedData.TerminalIDs = new TerminalId[] {new TerminalId {Id = 1}};
    			decryptedData.TransactionNumbers = new TransactionNumber[] {new TransactionNumber { TransNumber = 4204579}};		//orig. number too big = UInt!
    			decryptedData.Tos = new To[] {new	 To { Accountnumber = 550550556, Amount = 10, ServiceId = 100000}};
    
    			SerializeDecryptedTerminalData(decryptedData);
    
    			Console.ReadKey();
    		}
    
    		private static void SerializeDecryptedTerminalData(DecryptedTerminalData decryptedData) {
    			XmlSerializer serializer = new XmlSerializer(typeof(DecryptedTerminalData));
    			StreamWriter writer = new StreamWriter(_PATH);
    			serializer.Serialize(writer, decryptedData);
    			writer.Close();
    		}
    	}
    
    	    [XmlRoot("request")]
        public  class DecryptedTerminalData {
            [XmlAttribute("type")]
            public int Type { get; set; }
    
            [XmlAttribute("terminal-id")]
            public int Id { get; set; }
    
            [XmlAttribute("login")]
            public string Login { get; set; }
    
            [XmlElement("terminal-id")]
            public TerminalId[] TerminalIDs { get; set; }
    
            [XmlElement("transaction-number")]
            public TransactionNumber[] TransactionNumbers { get; set; }
            
            [XmlElement("to")]
            public To[] Tos { get; set; }
           
            public DecryptedTerminalData() {
                TerminalIDs = null;
                TransactionNumbers = null;
                Tos = null;
            }
       }
    
        public class TerminalId {
            public int Id;
            //{ get; set; }
            public TerminalId() {
            }
        }
    
        public  class TransactionNumber {
           // private XElement root = XElement.Parse(xml);
            public int TransNumber;
    
            public TransactionNumber() {
            }
        }
    
        public class To {
            [XmlElement("service-id")]
            public int ServiceId;
            //{ get; set; }
    
            [XmlElement("account-number")]
            public int Accountnumber;
            //{ get; set; }
    
            [XmlElement("amount")]
            public int Amount;
            //{ get; set; }
            public To() {
            }
        }
    }
    
    

    Hope it helps,

    wizend

    Tuesday, June 25, 2013 6:42 PM

All replies

  • I find a similar thread about your question,  the thread said you should change XMLAttribute to XMLElement, for more information, you can refer to the link: http://stackoverflow.com/questions/4252225/why-deserialize-xml-into-object-return-null-value


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample


    • Edited by Learning hard Friday, June 21, 2013 3:39 AM update
    • Marked as answer by maddyboss Friday, May 13, 2016 2:42 PM
    Friday, June 21, 2013 3:34 AM
  • The XML Attribute inidicate typeof(TrasactionNumber) while the Type for TransactionNumbers is TransactionNumber[]


    [XmlElement("transaction-number", Type = typeof(TransactionNumber))]
     public TransactionNumber[] TransactionNumbers { get; set; }

    Friday, June 21, 2013 4:46 AM
  • it is still returning the null value. Any help in code will be more helpful.

    I am getting following response in XML

    <DecryptedTerminalData>
    <Id>1</Id>
    <Login>A9ABC577-94D6-48EC-BB7C-00C525CA55F9</Login>
    <TerminalIDs>
    <TerminalId>
    <Id>0</Id>
    </TerminalId>
    </TerminalIDs>
    <Tos>
    <To>
    <Accountnumber>550550556</Accountnumber>
    <Amount>10</Amount>
    <ServiceId>100000</ServiceId>
    </To>
    </Tos>
    <TransactionNumbers>
    <TransactionNumber>
    <TransNumber>0</TransNumber>
    </TransactionNumber>
    </TransactionNumbers>
    <Type>9</Type>
    </DecryptedTerminalData>

    Sunday, June 23, 2013 8:38 AM
  • The following results in the output below:

    <?xml version="1.0" encoding="utf-8"?>
    <request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="9" te
    rminal-id="1" login="A9ABC577-94D6-48EC-BB7C-00C525CA55F9">
      <terminal-id>
        <Id>1</Id>
      </terminal-id>
      <transaction-number>
        <TransNumber>4204579</TransNumber>
      </transaction-number>
      <to>
        <service-id>100000</service-id>
        <account-number>550550556</account-number>
        <amount>10</amount>
      </to>
    </request>
    
    

    It's not exactly what you have in mind, but not far from that. Note that the type int is not bit enough for a transaction number like that in your example above (4204579018), for such a big number it needs to be uint.

    Here is my code, it should give you a direction at least:

    using System;
    using System.Xml.Serialization;
    using System.IO;
    
    namespace XmlSerializing_1 {
    	class Program {
    		static string _PATH = Path.Combine(Path.GetTempPath(), "Test\\terminalData.xml");
    		static void Main(string[] args) {
    			DecryptedTerminalData decryptedData = new DecryptedTerminalData();
    			decryptedData.Id = 1;
    			decryptedData.Login = "A9ABC577-94D6-48EC-BB7C-00C525CA55F9";
    			decryptedData.Type = 9;
    			decryptedData.TerminalIDs = new TerminalId[] {new TerminalId {Id = 1}};
    			decryptedData.TransactionNumbers = new TransactionNumber[] {new TransactionNumber { TransNumber = 4204579}};		//orig. number too big = UInt!
    			decryptedData.Tos = new To[] {new	 To { Accountnumber = 550550556, Amount = 10, ServiceId = 100000}};
    
    			SerializeDecryptedTerminalData(decryptedData);
    
    			Console.ReadKey();
    		}
    
    		private static void SerializeDecryptedTerminalData(DecryptedTerminalData decryptedData) {
    			XmlSerializer serializer = new XmlSerializer(typeof(DecryptedTerminalData));
    			StreamWriter writer = new StreamWriter(_PATH);
    			serializer.Serialize(writer, decryptedData);
    			writer.Close();
    		}
    	}
    
    	    [XmlRoot("request")]
        public  class DecryptedTerminalData {
            [XmlAttribute("type")]
            public int Type { get; set; }
    
            [XmlAttribute("terminal-id")]
            public int Id { get; set; }
    
            [XmlAttribute("login")]
            public string Login { get; set; }
    
            [XmlElement("terminal-id")]
            public TerminalId[] TerminalIDs { get; set; }
    
            [XmlElement("transaction-number")]
            public TransactionNumber[] TransactionNumbers { get; set; }
            
            [XmlElement("to")]
            public To[] Tos { get; set; }
           
            public DecryptedTerminalData() {
                TerminalIDs = null;
                TransactionNumbers = null;
                Tos = null;
            }
       }
    
        public class TerminalId {
            public int Id;
            //{ get; set; }
            public TerminalId() {
            }
        }
    
        public  class TransactionNumber {
           // private XElement root = XElement.Parse(xml);
            public int TransNumber;
    
            public TransactionNumber() {
            }
        }
    
        public class To {
            [XmlElement("service-id")]
            public int ServiceId;
            //{ get; set; }
    
            [XmlElement("account-number")]
            public int Accountnumber;
            //{ get; set; }
    
            [XmlElement("amount")]
            public int Amount;
            //{ get; set; }
            public To() {
            }
        }
    }
    
    

    Hope it helps,

    wizend

    Tuesday, June 25, 2013 6:42 PM