Как преобразовать приведенный ниже ответ api в формате xml в класс c#? - PullRequest
0 голосов
/ 29 мая 2020

Я разместил вопрос о том, как вызвать api в формате xml.
Вызов api в формате xml в c# - Получение ошибки «Удаленный сертификат недействителен в соответствии с проверкой. procedure "

Теперь я получаю ответ от api. Но теперь я хочу преобразовать ответ в объект c#. Атрибуты xml меняются в случае успеха или неудачи.

Ниже ответ, если все прошло успешно ..

<FIXML xsi:schemaLocation="http://www.finacle.com/fixml BalInq.xsd" xmlns="http://www.finacle.com/fixml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Header> <ResponseHeader> <RequestMessageKey> <RequestUUID>Req_1560515797477</RequestUUID> <ServiceRequestId>BalInq</ServiceRequestId> <ServiceRequestVersion>10.2</ServiceRequestVersion> <ChannelId>COR</ChannelId> </RequestMessageKey> <ResponseMessageInfo> <BankId>01</BankId> <TimeZone>GMT+05:30</TimeZone> <MessageDateTime>2020-05-29T12:39:13.379</MessageDateTime> </ResponseMessageInfo><UBUSTransaction> <Id/> <Status/> </UBUSTransaction> <HostTransaction> <Id/> <Status>SUCCESS</Status> </HostTransaction> <HostParentTransaction> <Id/> <Status/> </HostParentTransaction> <CustomInfo/> </ResponseHeader> </Header> <Body> <BalInqResponse> <BalInqRs> <AcctId> <AcctId>4362500100484501</AcctId> <AcctType> <SchmCode></SchmCode> <SchmType></SchmType> </AcctType> <AcctCurr>INR</AcctCurr> <BankInfo> <BankId></BankId> <Name></Name> <BranchId>436</BranchId> <BranchName></BranchName> <PostAddr> <Addr1></Addr1> <Addr2></Addr2> <Addr3></Addr3> <City></City> <StateProv></StateProv> <PostalCode></PostalCode> <Country></Country> <AddrType></AddrType> </PostAddr> </BankInfo> </AcctId> <AcctBal> <BalType>CLRBAL</BalType> <BalAmt> <amountValue>2.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>SANLIM</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>UTLAMT</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>DRWPWR</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>ADHOC</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>SINTRN</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>CLNADH</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>DACC</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>LIEN</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>SYSRES</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>OVDFUT</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>DAFALM</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>FUTBAL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>FLOAT</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>AVAIL</BalType> <BalAmt> <amountValue>2.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>FFDAVL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>EFFAVL</BalType> <BalAmt> <amountValue>2.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>EFUAVL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode></currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>EMRADV</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>CLNEMR</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>CLNSTR</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>SYSGENLM</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>UNCLRBAL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>POOLAVL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>HOMAVL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>FUTCR</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>UTILFUTBAL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>USEDOC</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode>INR</currencyCode> </BalAmt> </AcctBal> <AcctBal> <BalType>DAFAWITHDRAWL</BalType> <BalAmt> <amountValue>0.00</amountValue> <currencyCode></currencyCode> </BalAmt> </AcctBal> </BalInqRs><BalInq_CustomData/> </BalInqResponse></Body></FIXML>

Ниже ответ в случае сбоя

<FIXML xsi:schemaLocation="http://www.finacle.com/fixml BalInq.xsd" xmlns="http://www.finacle.com/fixml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Header>
<ResponseHeader>
<RequestMessageKey>
<RequestUUID>Req_1560515797477</RequestUUID>
<ServiceRequestId>BalInq</ServiceRequestId>
<ServiceRequestVersion>10.2</ServiceRequestVersion>
<ChannelId>COR</ChannelId>
</RequestMessageKey>
<ResponseMessageInfo>
<BankId>01</BankId>
<TimeZone></TimeZone>
<MessageDateTime>2020-05-29T07:28:59.421</MessageDateTime>
</ResponseMessageInfo>
<UBUSTransaction>
<Id>null</Id>
<Status>FAILED</Status>
</UBUSTransaction>
<HostTransaction>
<Id>0000</Id>
<Status>FAILURE</Status>
</HostTransaction>
<HostParentTransaction>
<Id>null</Id>
<Status>null</Status>
</HostParentTransaction>
<CustomInfo/>
</ResponseHeader>
</Header>
<Body>
<Error>
<FIBusinessException>
<ErrorDetail><ErrorCode>162</ErrorCode><ErrorDesc>Account does not exist</ErrorDesc><ErrorSource>acct.foracid</ErrorSource><ErrorType>BE</ErrorType></ErrorDetail>
</FIBusinessException>
</Error></Body>
</FIXML>

Как мне преобразовать его c# объекты ?. Потому что атрибут меняется.

1 Ответ

0 голосов
/ 29 мая 2020

Создайте два класса, один для успешного и один для неудачного сообщения. В зависимости от Status Code разобрать ответ на соответствующий класс. Если вы думаете о создании динамических классов, вы можете попробовать использовать ключевое слово dynamic, но C# - это язык строгого типа. Кроме того, есть небольшой инструмент, который может помочь вам создать класс из ответа, попробуйте, вот ссылка .

Итак, связь между вашими статусами - это часть заголовка , поэтому я разделил его на один класс.

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
   public class HeaderDto
   {

       [XmlRoot(ElementName = "RequestMessageKey", Namespace = "http://www.finacle.com/fixml")]
       public class RequestMessageKey
       {
           [XmlElement(ElementName = "RequestUUID", Namespace = "http://www.finacle.com/fixml")]
           public string RequestUUID { get; set; }
           [XmlElement(ElementName = "ServiceRequestId", Namespace = "http://www.finacle.com/fixml")]
           public string ServiceRequestId { get; set; }
           [XmlElement(ElementName = "ServiceRequestVersion", Namespace = "http://www.finacle.com/fixml")]
           public string ServiceRequestVersion { get; set; }
           [XmlElement(ElementName = "ChannelId", Namespace = "http://www.finacle.com/fixml")]
           public string ChannelId { get; set; }
       }

       [XmlRoot(ElementName = "ResponseMessageInfo", Namespace = "http://www.finacle.com/fixml")]
       public class ResponseMessageInfo
       {
           [XmlElement(ElementName = "BankId", Namespace = "http://www.finacle.com/fixml")]
           public string BankId { get; set; }
           [XmlElement(ElementName = "TimeZone", Namespace = "http://www.finacle.com/fixml")]
           public string TimeZone { get; set; }
           [XmlElement(ElementName = "MessageDateTime", Namespace = "http://www.finacle.com/fixml")]
           public string MessageDateTime { get; set; }
       }

       [XmlRoot(ElementName = "UBUSTransaction", Namespace = "http://www.finacle.com/fixml")]
       public class UBUSTransaction
       {
           [XmlElement(ElementName = "Id", Namespace = "http://www.finacle.com/fixml")]
           public string Id { get; set; }
           [XmlElement(ElementName = "Status", Namespace = "http://www.finacle.com/fixml")]
           public string Status { get; set; }
       }

       [XmlRoot(ElementName = "HostTransaction", Namespace = "http://www.finacle.com/fixml")]
       public class HostTransaction
       {
           [XmlElement(ElementName = "Id", Namespace = "http://www.finacle.com/fixml")]
           public string Id { get; set; }
           [XmlElement(ElementName = "Status", Namespace = "http://www.finacle.com/fixml")]
           public string Status { get; set; }
       }

       [XmlRoot(ElementName = "HostParentTransaction", Namespace = "http://www.finacle.com/fixml")]
       public class HostParentTransaction
       {
           [XmlElement(ElementName = "Id", Namespace = "http://www.finacle.com/fixml")]
           public string Id { get; set; }
           [XmlElement(ElementName = "Status", Namespace = "http://www.finacle.com/fixml")]
           public string Status { get; set; }
       }

       [XmlRoot(ElementName = "ResponseHeader", Namespace = "http://www.finacle.com/fixml")]
       public class ResponseHeader
       {
           [XmlElement(ElementName = "RequestMessageKey", Namespace = "http://www.finacle.com/fixml")]
           public RequestMessageKey RequestMessageKey { get; set; }
           [XmlElement(ElementName = "ResponseMessageInfo", Namespace = "http://www.finacle.com/fixml")]
           public ResponseMessageInfo ResponseMessageInfo { get; set; }
           [XmlElement(ElementName = "UBUSTransaction", Namespace = "http://www.finacle.com/fixml")]
           public UBUSTransaction UBUSTransaction { get; set; }
           [XmlElement(ElementName = "HostTransaction", Namespace = "http://www.finacle.com/fixml")]
           public HostTransaction HostTransaction { get; set; }
           [XmlElement(ElementName = "HostParentTransaction", Namespace = "http://www.finacle.com/fixml")]
           public HostParentTransaction HostParentTransaction { get; set; }
           [XmlElement(ElementName = "CustomInfo", Namespace = "http://www.finacle.com/fixml")]
           public string CustomInfo { get; set; }
       }

       [XmlRoot(ElementName = "Header", Namespace = "http://www.finacle.com/fixml")]
       public class Header
       {
           [XmlElement(ElementName = "ResponseHeader", Namespace = "http://www.finacle.com/fixml")]
           public ResponseHeader ResponseHeader { get; set; }
       }
   }
}

Класс ответа Success будет выглядеть примерно так ...

/* 
 Licensed under the Apache License, Version 2.0

 http://www.apache.org/licenses/LICENSE-2.0
 */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using static Xml2CSharp.HeaderDto;

namespace Xml2CSharp
{
    public class SuccessDto
    {
        public RequestMessageKey RequestMessageKey { get; set; }
        public ResponseMessageInfo ResponseMessageInfo { get; set; }
        public UBUSTransaction UBUSTransaction { get; set; }
        public HostTransaction HostTransaction { get; set; }
        public HostParentTransaction HostParentTransaction { get; set; }
        public ResponseHeader ResponseHeader { get; set; }
        public Header Header { get; set; }

        [XmlRoot(ElementName = "AcctType", Namespace = "http://www.finacle.com/fixml")]
        public class AcctType
        {
            [XmlElement(ElementName = "SchmCode", Namespace = "http://www.finacle.com/fixml")]
            public string SchmCode { get; set; }
            [XmlElement(ElementName = "SchmType", Namespace = "http://www.finacle.com/fixml")]
            public string SchmType { get; set; }
        }

        [XmlRoot(ElementName = "PostAddr", Namespace = "http://www.finacle.com/fixml")]
        public class PostAddr
        {
            [XmlElement(ElementName = "Addr1", Namespace = "http://www.finacle.com/fixml")]
            public string Addr1 { get; set; }
            [XmlElement(ElementName = "Addr2", Namespace = "http://www.finacle.com/fixml")]
            public string Addr2 { get; set; }
            [XmlElement(ElementName = "Addr3", Namespace = "http://www.finacle.com/fixml")]
            public string Addr3 { get; set; }
            [XmlElement(ElementName = "City", Namespace = "http://www.finacle.com/fixml")]
            public string City { get; set; }
            [XmlElement(ElementName = "StateProv", Namespace = "http://www.finacle.com/fixml")]
            public string StateProv { get; set; }
            [XmlElement(ElementName = "PostalCode", Namespace = "http://www.finacle.com/fixml")]
            public string PostalCode { get; set; }
            [XmlElement(ElementName = "Country", Namespace = "http://www.finacle.com/fixml")]
            public string Country { get; set; }
            [XmlElement(ElementName = "AddrType", Namespace = "http://www.finacle.com/fixml")]
            public string AddrType { get; set; }
        }

        [XmlRoot(ElementName = "BankInfo", Namespace = "http://www.finacle.com/fixml")]
        public class BankInfo
        {
            [XmlElement(ElementName = "BankId", Namespace = "http://www.finacle.com/fixml")]
            public string BankId { get; set; }
            [XmlElement(ElementName = "Name", Namespace = "http://www.finacle.com/fixml")]
            public string Name { get; set; }
            [XmlElement(ElementName = "BranchId", Namespace = "http://www.finacle.com/fixml")]
            public string BranchId { get; set; }
            [XmlElement(ElementName = "BranchName", Namespace = "http://www.finacle.com/fixml")]
            public string BranchName { get; set; }
            [XmlElement(ElementName = "PostAddr", Namespace = "http://www.finacle.com/fixml")]
            public PostAddr PostAddr { get; set; }
        }

        [XmlRoot(ElementName = "AcctId", Namespace = "http://www.finacle.com/fixml")]
        public class AcctId
        {
            [XmlElement(ElementName = "AcctId", Namespace = "http://www.finacle.com/fixml")]
            public string AcctId { get; set; }
            [XmlElement(ElementName = "AcctType", Namespace = "http://www.finacle.com/fixml")]
            public AcctType AcctType { get; set; }
            [XmlElement(ElementName = "AcctCurr", Namespace = "http://www.finacle.com/fixml")]
            public string AcctCurr { get; set; }
            [XmlElement(ElementName = "BankInfo", Namespace = "http://www.finacle.com/fixml")]
            public BankInfo BankInfo { get; set; }
        }

        [XmlRoot(ElementName = "BalAmt", Namespace = "http://www.finacle.com/fixml")]
        public class BalAmt
        {
            [XmlElement(ElementName = "amountValue", Namespace = "http://www.finacle.com/fixml")]
            public string AmountValue { get; set; }
            [XmlElement(ElementName = "currencyCode", Namespace = "http://www.finacle.com/fixml")]
            public string CurrencyCode { get; set; }
        }

        [XmlRoot(ElementName = "AcctBal", Namespace = "http://www.finacle.com/fixml")]
        public class AcctBal
        {
            [XmlElement(ElementName = "BalType", Namespace = "http://www.finacle.com/fixml")]
            public string BalType { get; set; }
            [XmlElement(ElementName = "BalAmt", Namespace = "http://www.finacle.com/fixml")]
            public BalAmt BalAmt { get; set; }
        }

        [XmlRoot(ElementName = "BalInqRs", Namespace = "http://www.finacle.com/fixml")]
        public class BalInqRs
        {
            [XmlElement(ElementName = "AcctId", Namespace = "http://www.finacle.com/fixml")]
            public AcctId AcctId { get; set; }
            [XmlElement(ElementName = "AcctBal", Namespace = "http://www.finacle.com/fixml")]
            public List<AcctBal> AcctBal { get; set; }
        }

        [XmlRoot(ElementName = "BalInqResponse", Namespace = "http://www.finacle.com/fixml")]
        public class BalInqResponse
        {
            [XmlElement(ElementName = "BalInqRs", Namespace = "http://www.finacle.com/fixml")]
            public BalInqRs BalInqRs { get; set; }
            [XmlElement(ElementName = "BalInq_CustomData", Namespace = "http://www.finacle.com/fixml")]
            public string BalInq_CustomData { get; set; }
        }

        [XmlRoot(ElementName = "Body", Namespace = "http://www.finacle.com/fixml")]
        public class Body
        {
            [XmlElement(ElementName = "BalInqResponse", Namespace = "http://www.finacle.com/fixml")]
            public BalInqResponse BalInqResponse { get; set; }
        }

        [XmlRoot(ElementName = "FIXML", Namespace = "http://www.finacle.com/fixml")]
        public class FIXML
        {
            [XmlElement(ElementName = "Header", Namespace = "http://www.finacle.com/fixml")]
            public Header Header { get; set; }
            [XmlElement(ElementName = "Body", Namespace = "http://www.finacle.com/fixml")]
            public Body Body { get; set; }
            [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
            public string SchemaLocation { get; set; }
            [XmlAttribute(AttributeName = "xmlns")]
            public string Xmlns { get; set; }
            [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Xsi { get; set; }
        }
    }

}

А класс Failed будет таким

/* 
 Licensed under the Apache License, Version 2.0

 http://www.apache.org/licenses/LICENSE-2.0
 */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using static Xml2CSharp.HeaderDto;

namespace Xml2CSharp
{
    public class FailedDto
    {
        public RequestMessageKey RequestMessageKey { get; set; }
        public ResponseMessageInfo ResponseMessageInfo { get; set; }
        public UBUSTransaction UBUSTransaction { get; set; }
        public HostTransaction HostTransaction { get; set; }
        public HostParentTransaction HostParentTransaction { get; set; }
        public ResponseHeader ResponseHeader { get; set; }
        public Header Header { get; set; }


        [XmlRoot(ElementName = "ErrorDetail", Namespace = "http://www.finacle.com/fixml")]
        public class ErrorDetail
        {
            [XmlElement(ElementName = "ErrorCode", Namespace = "http://www.finacle.com/fixml")]
            public string ErrorCode { get; set; }
            [XmlElement(ElementName = "ErrorDesc", Namespace = "http://www.finacle.com/fixml")]
            public string ErrorDesc { get; set; }
            [XmlElement(ElementName = "ErrorSource", Namespace = "http://www.finacle.com/fixml")]
            public string ErrorSource { get; set; }
            [XmlElement(ElementName = "ErrorType", Namespace = "http://www.finacle.com/fixml")]
            public string ErrorType { get; set; }
        }

        [XmlRoot(ElementName = "FIBusinessException", Namespace = "http://www.finacle.com/fixml")]
        public class FIBusinessException
        {
            [XmlElement(ElementName = "ErrorDetail", Namespace = "http://www.finacle.com/fixml")]
            public ErrorDetail ErrorDetail { get; set; }
        }

        [XmlRoot(ElementName = "Error", Namespace = "http://www.finacle.com/fixml")]
        public class Error
        {
            [XmlElement(ElementName = "FIBusinessException", Namespace = "http://www.finacle.com/fixml")]
            public FIBusinessException FIBusinessException { get; set; }
        }

        [XmlRoot(ElementName = "Body", Namespace = "http://www.finacle.com/fixml")]
        public class Body
        {
            [XmlElement(ElementName = "Error", Namespace = "http://www.finacle.com/fixml")]
            public Error Error { get; set; }
        }

        [XmlRoot(ElementName = "FIXML", Namespace = "http://www.finacle.com/fixml")]
        public class FIXML
        {
            [XmlElement(ElementName = "Header", Namespace = "http://www.finacle.com/fixml")]
            public Header Header { get; set; }
            [XmlElement(ElementName = "Body", Namespace = "http://www.finacle.com/fixml")]
            public Body Body { get; set; }
            [XmlAttribute(AttributeName = "schemaLocation", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
            public string SchemaLocation { get; set; }
            [XmlAttribute(AttributeName = "xmlns")]
            public string Xmlns { get; set; }
            [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Xsi { get; set; }
        }
    }
}

Считайте это псевдокодом, поскольку он не тестировался. Поэтому, когда вы получите ответ, проанализируйте заголовок и прочитайте узел, который вам нужен, чтобы проверить, является ли его сообщение Success или Failed, а затем просто проанализируйте его как этот объект.

...