xml - Restsharp反序列化不同的属性名称
问题描述
我有一个定义为这样的类:
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderResponseBody
{
[XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Fault Fault { get; set; }
[XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
public IPGApiOrderResponse IPGApiOrderResponse { get; set; }
}
并且它被正确反序列化。
但是现在,我需要将属性更改IPGApiOrderResponse
为其他内容,例如Response
不更改 XML 属性,如下所示:
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderResponseBody
{
[XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Fault Fault { get; set; }
[XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
public IPGApiOrderResponse Response { get; set; }
}
但是,更改属性名称后,反序列化时始终为空。我错过了什么?
编辑 1:这是我试图反序列化的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header />
<SOAP-ENV:Body>
<ipgapi:IPGApiOrderResponse xmlns:ipgapi="http://ipg-online.com/ipgapi/schemas/ipgapi" xmlns:a1="http://ipg-online.com/ipgapi/schemas/a1" xmlns:v1="http://ipg-online.com/ipgapi/schemas/v1">
<ipgapi:ApprovalCode>Y:827973:4523631795:PPX :497049</ipgapi:ApprovalCode>
<ipgapi:AVSResponse>PPX</ipgapi:AVSResponse>
<ipgapi:Brand>MASTERCARD</ipgapi:Brand>
<ipgapi:Country>BRA</ipgapi:Country>
<ipgapi:CommercialServiceProvider>BIN</ipgapi:CommercialServiceProvider>
<ipgapi:OrderId>A1556224292156889196</ipgapi:OrderId>
<ipgapi:IpgTransactionId>84523631795</ipgapi:IpgTransactionId>
<ipgapi:PaymentType>CREDITCARD</ipgapi:PaymentType>
<ipgapi:ProcessorApprovalCode>827973</ipgapi:ProcessorApprovalCode>
<ipgapi:ProcessorReferenceNumber>000067462</ipgapi:ProcessorReferenceNumber>
<ipgapi:ProcessorResponseCode>00</ipgapi:ProcessorResponseCode>
<ipgapi:ProcessorResponseMessage>APROVADA 000067462</ipgapi:ProcessorResponseMessage>
<ipgapi:TDate>1556224292</ipgapi:TDate>
<ipgapi:TDateFormatted>2019.04.25 22:31:32 (CEST)</ipgapi:TDateFormatted>
<ipgapi:TerminalID>EII00714</ipgapi:TerminalID>
<ipgapi:TransactionResult>APPROVED</ipgapi:TransactionResult>
<ipgapi:TransactionTime>1556224292</ipgapi:TransactionTime>
</ipgapi:IPGApiOrderResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
解决方案
以下作品:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(Envelope));
Envelope envelope = (Envelope)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class Envelope
{
[XmlElement("Body")]
public OrderResponseBody OrderResponseBody { get; set; }
}
[XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public class OrderResponseBody
{
[XmlElement(ElementName = "Fault", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public Fault Fault { get; set; }
[XmlElement(ElementName = "IPGApiOrderResponse", Namespace = "http://ipg-online.com/ipgapi/schemas/ipgapi")]
public IPGApiOrderResponse Response { get; set; }
}
public class IPGApiOrderResponse
{
public string ApprovalCode { get; set; }
}
public class Fault
{
}
}
推荐阅读
- java - 在 foreach 循环中的代码完成后关闭 Webdriver?
- mysql - MYSQL 5.7 表 3 字段 DATETIME '0000-00-00 00:00:00' 如何将新结构更改为 DEFAULT NULL
- node.js - 如何使用 socket.io 和 node 构建私人聊天信使
- python - 我们如何在一个 x 轴上绘制两个不同的字典,一个向上,另一个向下绘制,就像从头到尾的格式一样?
- graph - 是否可以在python的机器学习中找到数据集的x和y坐标中的移位值?
- batch-file - FINDSTR - 添加文件的“修改日期”作为每个结果的前缀
- go - 使用相同浮点常量值的不同模式会导致不同的结果
- node.js - 带有 express/db.json 的 json-server 未反映更改
- c# - 在选中的列表框中确定(取消)选中的复选框
- php - 使用字符串从 php 更改引导程序的内容