簡體   English   中英

即使POCO具有從xsd2code ++工具生成的正確xml屬性,XML反序列化也將返回空數組

[英]Xml deserialization returns empty array even though POCO has right xml attributes generated from xsd2code++ tool

我正在使用Visual Studio的XSD2Code ++ 2019工具從一組5個xsds生成POCO。 我在下面添加了POCO類。 我看到它具有正確的xml裝飾器,可以正確地序列化。 但是,我真的無法理解或弄清楚為什么返回的反序列化數據中的3級對象始終為空,並且沒有類型轉換為正確的類型。

我也嘗試過將屬性更改為xmlArray和xmlArrayElement,但是沒有一個起作用。

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

使用C#代碼反序列化后的示例XML響應具有空值

<?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>

反序列化的代碼-

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

此處QueryResponse沒有返回Invoice(類型為IntuitEntity)的對象。 而是返回空值。 查看截圖。 https://imgur.com/a/5yF6Khb

我真的需要幫助弄清楚為什么第3級屬性返回為空。

我在下面測試了代碼,它可以正常工作。 我手動生成了比使用工具更簡單的結果的類。

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; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM