0

I am using XSD2Code++ 2019 tool for visual studio to generate POCOs from aset of 5 xsds. I have added the POCO class below. I see that it has the right xml decorators for it to serialize properly. But I really fail to understand or figure out why the 3rd level object in the returned deserialized data is always empty and not typecasted to the correct type.

I have tried changing attributes to xmlArray and xmlArrayElement too but none of that worked.

POCO class- https://gist.github.com/nimisha84/b86a4bb2bf37aea6ec351a9f6e331bed

Sample xml response which has null values after deserialization using c# code-

<?xml version="1.0" encoding="UTF-8"?>
    <IntuitResponse xmlns="http://schema.intuit.com/finance/v3" time="2019-07-05T14:29:08.603-07:00">
   <QueryResponse startPosition="1" maxResults="1" totalCount="1">
  <Invoice domain="QBO" sparse="false">
     <Id>8633</Id>
     <SyncToken>14</SyncToken>
     <MetaData>
        <CreateTime>2019-01-09T11:32:12-08:00</CreateTime>
        <LastUpdatedTime>2019-06-05T12:49:40-07:00</LastUpdatedTime>
     </MetaData>
     <CustomField>
        <DefinitionId>1</DefinitionId>
        <Name>CustomPO</Name>
        <Type>StringType</Type>
        <StringValue>Gold</StringValue>
     </CustomField>
     <DocNumber>2830</DocNumber>
     <TxnDate>2019-01-09</TxnDate>
     <CurrencyRef name="United States Dollar">USD</CurrencyRef>
     <ExchangeRate>1</ExchangeRate>
     <PrivateNote>Voided - Voided</PrivateNote>
     <Line>
        <Id>1</Id>
        <LineNum>1</LineNum>
        <Description>Description</Description>
        <Amount>0</Amount>
        <DetailType>SalesItemLineDetail</DetailType>
        <SalesItemLineDetail>
           <ItemRef name="Name27140">815</ItemRef>
           <Qty>0</Qty>
           <TaxCodeRef>NON</TaxCodeRef>
        </SalesItemLineDetail>
     </Line>
     <Line>
        <Amount>0</Amount>
        <DetailType>SubTotalLineDetail</DetailType>
        <SubTotalLineDetail />
     </Line>
     <TxnTaxDetail>
        <TotalTax>0</TotalTax>
     </TxnTaxDetail>
     <CustomerRef name="a4">2561</CustomerRef>
     <DueDate>2019-01-09</DueDate>
     <TotalAmt>0</TotalAmt>
     <HomeTotalAmt>0</HomeTotalAmt>
     <ApplyTaxAfterDiscount>false</ApplyTaxAfterDiscount>
     <PrintStatus>NeedToPrint</PrintStatus>
     <EmailStatus>NotSet</EmailStatus>
     <Balance>0</Balance>
     <Deposit>0</Deposit>
     <AllowIPNPayment>false</AllowIPNPayment>
     <AllowOnlinePayment>false</AllowOnlinePayment>
     <AllowOnlineCreditCardPayment>false</AllowOnlineCreditCardPayment>
     <AllowOnlineACHPayment>false</AllowOnlineACHPayment>
  </Invoice>
   </QueryResponse>
</IntuitResponse>

Code to deserialize-

string responseText = apiResponse.ReadToEnd();
var responseSerializer = new XmlObjectSerializer();
IntuitResponse restResponse = 
(IntuitResponse)this.responseSerializer.Deserialize<IntuitResponse>(responseText);
res=restResponse.Items[0] as QueryResponse;

here QueryResponse is not having Invoice(of type IntuitEntity) object returned. Instead empty value is returned. See screenshot. https://i.stack.imgur.com/ZgowG.jpg

I really need help to figure out why the 3rd level property is returned as empty.

Nemo
  • 1
  • 1
  • 1
    You 2nd answer was deleted by Bhargav Rao a couple of hour ago. When you use in xml serialization XmlArrayItem you need two levels of xml tags like Lines and Line. Your code only has one tag so you need to use XmlElement with just Line in my solution. – jdweng Jul 08 '19 at 02:15
  • @jdwend tried your suggestion and also did a compare with a class which Visual Studio generates. Found the issue to be with base class not translating to derived ones even though XmlInclude tag is present. Have to still research why that is happening. If I add all Include types of IntuitEntity directly to QueryResponse List, it works fine but not if the direct XmlElement of IntuitEntity is present. It might be an issue with List type not transalting properly. Will check more, – nimisha shrivastava Jul 09 '19 at 20:00

1 Answers1

0

I tested code below and it works. I manually generated the classes which produces simpler results than using tools.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string responseText = File.ReadAllText(FILENAME);
            StringReader reader = new StringReader(responseText);

            XmlReader xReader = XmlReader.Create(reader);
            XmlSerializer serializer = new XmlSerializer(typeof(IntuitResponse));

            IntuitResponse response = (IntuitResponse)serializer.Deserialize(xReader);
        }
    }
    [XmlRoot(ElementName = "IntuitResponse", Namespace = "http://schema.intuit.com/finance/v3")]
    public class IntuitResponse
    {
        [XmlAttribute("time")]
        public DateTime time { get; set; }

        [XmlElement(ElementName = "QueryResponse", Namespace = "http://schema.intuit.com/finance/v3")]
        public QueryResponse response { get; set; }
    }

    public class QueryResponse
    {
        [XmlAttribute("startPosition")]
        public int startPosition { get; set; }
        [XmlAttribute("maxResults")]
        public int maxResults { get; set; }
        [XmlAttribute("totalCount")]
        public int totalCount { get; set; }

        [XmlElement(ElementName = "Invoice", Namespace = "http://schema.intuit.com/finance/v3")]
        public Invoice invoice { get; set; }
    }
    public class Invoice
    {

        [XmlAttribute("domain")]
        public string domain { get; set; }
        [XmlAttribute("sparse")]
        public Boolean sparse { get; set; }

        public int Id { get; set; }
        public int SyncToken { get; set; }

        [XmlElement(ElementName = "MetaData", Namespace = "http://schema.intuit.com/finance/v3")]
        public MetaData metaData { get; set; }

        [XmlElement(ElementName = "CustomField", Namespace = "http://schema.intuit.com/finance/v3")]
        public CustomField customField { get; set; }
        public int DocNumber { get; set; }
        public DateTime TxnDate { get; set; }

        [XmlElement(ElementName = "CurrencyRef", Namespace = "http://schema.intuit.com/finance/v3")]
        public CurrencyRef currencyRef { get; set; }
        public int ExchangeRate { get; set; }
        public string PrivateNote { get; set; }
        [XmlElement(ElementName = "Line", Namespace = "http://schema.intuit.com/finance/v3")]
        public List<Line> line { get; set; }

        [XmlElement(ElementName = "TxnTaxDetail", Namespace = "http://schema.intuit.com/finance/v3")]
        public TxnTaxDetail txnTaxDetail { get; set; }

        [XmlElement(ElementName = "CustomerRef", Namespace = "http://schema.intuit.com/finance/v3")]
        public CustomerRef CustomerRef { get; set; }

        public DateTime DueDate { get; set; }
        public int TotalAmt { get; set; }
        public int HomeTotalAmt { get; set; }
        public Boolean ApplyTaxAfterDiscount { get; set; }
        public string PrintStatus { get; set; }
        public string EmailStatus { get; set; }
        public int Balance { get; set; }
        public int Deposit { get; set; }
        public Boolean AllowIPNPayment { get; set; }
        public Boolean AllowOnlinePayment { get; set; }
        public Boolean AllowOnlineCreditCardPayment { get; set; }
        public Boolean AllowOnlineACHPayment { get; set; }
    }
    public class MetaData
    {
        public DateTime CreateTime { get; set; }
        public DateTime LastUpdatedTime { get; set; }
    }
    public class CustomField
    {

        public int DefinitionId { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string StringValue { get; set; }
    }
    public class CurrencyRef
    {
        [XmlAttribute("name")]
        public string name { get; set; }

        [XmlText]
        public string value { get; set; }

    }
    public class Line
    {
        public int Id { get; set; }
        public int LineNum { get; set; }
        public string Description { get; set; }
        public decimal Amount { get; set; }
        public string DetailType { get; set; }

        [XmlElement(ElementName = "SalesItemLineDetail", Namespace = "http://schema.intuit.com/finance/v3")]
        public SalesItemLineDetail salesItemLineDetail { get; set; }
    }
    public class CustomerRef
    {
        [XmlAttribute("name")]
        public string name { get; set; }

        [XmlText]
        public string value { get; set; }
    }
    public class SalesItemLineDetail
    {
        [XmlElement(ElementName = "ItemRef", Namespace = "http://schema.intuit.com/finance/v3")]
        public ItemRef itemRef { get; set; }

        public int Qty { get; set; }
        public string TaxCodeRef { get; set; }
    }
    public class ItemRef
    {
        [XmlAttribute("name")]
        public string name { get; set; }

        [XmlText]
        public string value { get; set; }
    }
    public class TxnTaxDetail
    {
        public int TotalTax { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20