如何将xml格式的api响应下转换为c#类? [英] How to convert below api response which is in xml format to c# class?

查看:67
本文介绍了如何将xml格式的api响应下转换为c#类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发布了一个有关如何调用xml格式的api的问题.
在xml中调用api c#中的格式-获取错误根据验证程序,远程证书无效"

I had posted a question regarding how to call api which is in xml format.
Calling api in xml format in c# - Getting error "Remote certificate is invalid according to the validation procedure"

现在我正在从api获得响应.但是现在我想将响应转换为c#对象. xml的属性在成功和失败的情况下都会更改.

Now I am getting a response from the api. But now I want to convert the response to c# object. The attributes of xml changes in case of success and failure.

如果一切成功,请做出回应.

<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#对象?因为属性发生变化.

How do I convert it c# objects?. Because attribute changes.

推荐答案

创建两个类,一个用于成功,一个用于失败消息.根据Status Code解析对相应类的响应. 如果您正在考虑创建动态类,则可以尝试使用dynamic关键字,但是C#是强类型语言. 另外,还有一个小工具可以帮助您从响应中创建类,请尝试一下,这里是链接

Create two classes, one for Success and one for Failed message. Depending on Status Code parse the response to corresponding class. If you're thinking to create a dynamical class(es), well you could try using dynamic keyword, but C# is strong type language. Also, there is a small tool who can help you create the class from the response, give it a try, here is the link.

因此,您的状态之间的粘合点是标题部分,因此我将其分为一个类.

So the glue between your statuses is the header part, so I have separated it in one class.

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

成功响应类如下所示……

The Success response class would look something like this...

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

}

失败的类会是这样

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

由于未经测试,请将此视为伪代码. 因此,当您获得响应时,解析标头,然后读取您需要验证其成功还是失败消息的节点,然后将其解析为该对象.

Please consider this as pseudo code, since it's not tested. So when you get the response, parse the header, and read the node you need to validate if its Success or Failed message, then just parse it as that object.

这篇关于如何将xml格式的api响应下转换为c#类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆