c# - System.InvalidOperationException。指定类型未被识别错误反序列化 XML
问题描述
我正在尝试使用 C# 反序列化以下 XML:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<out:response xmlns:out="http://AccountService" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<fault>
<faultcode>0</faultcode>
<faultstring>RESPONSE_OK</faultstring>
<detail>Successful reply.</detail>
</fault>
<serviceResponse xsi:type="out:GetAccountInfoResponse">
<accountInfo>
<personId>123</personId>
<account>
<accountId>1</accountId>
<accountNo>11</accountNo>
</account>
<account>
<accountId>2</accountId>
<accountNo>22</accountNo>
</account>
</accountInfo>
</serviceResponse>
</out:response>
</soapenv:Body>
</soapenv:Envelope>
我的班级结构:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private response responseField;
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://AccountService")]
public response response
{
get
{
return this.responseField;
}
set
{
this.responseField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://AccountService")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://AccountService", IsNullable = false)]
public partial class response
{
private fault faultField;
private serviceResponse serviceResponseField;
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public fault fault
{
get
{
return this.faultField;
}
set
{
this.faultField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute(Namespace = "")]
public serviceResponse serviceResponse
{
get
{
return this.serviceResponseField;
}
set
{
this.serviceResponseField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class fault
{
private byte faultcodeField;
private string faultstringField;
private string detailField;
/// <remarks/>
public byte faultcode
{
get
{
return this.faultcodeField;
}
set
{
this.faultcodeField = value;
}
}
public string faultstring
{
get
{
return this.faultstringField;
}
set
{
this.faultstringField = value;
}
}
public string detail
{
get
{
return this.detailField;
}
set
{
this.detailField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class serviceResponse
{
private serviceResponseAccountInfo accountInfoField;
public serviceResponseAccountInfo accountInfo
{
get
{
return this.accountInfoField;
}
set
{
this.accountInfoField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class serviceResponseAccountInfo
{
private byte personIdField;
private serviceResponseAccountInfoAccount[] accountField;
/// <remarks/>
public byte personId
{
get
{
return this.personIdField;
}
set
{
this.personIdField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("account")]
public serviceResponseAccountInfoAccount[] account
{
get
{
return this.accountField;
}
set
{
this.accountField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class serviceResponseAccountInfoAccount
{
private byte accountIdField;
private byte accountNoField;
public byte accountId
{
get
{
return this.accountIdField;
}
set
{
this.accountIdField = value;
}
}
public byte accountNo
{
get
{
return this.accountNoField;
}
set
{
this.accountNoField = value;
}
}
}
}
反序列化产生错误:
无法识别指定的类型:name='GetAccountInfoResponse'。
解决方案
在 xml 模式中定义特定类型时:
<serviceResponse xsi:type="out:GetAccountInfoResponse">
您必须在serviceResponse类的 XmlTypeAttribute 中指定它,这样
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://AccountService",TypeName = "GetAccountInfoResponse")]
推荐阅读
- python - 使用两个下拉按钮构建一个散点图,一个用于 x 轴,一个用于 y 轴
- vim - 在 Vim 中,数字键盘上的 5 键在当前行上方插入一行,并带有 "E"
- dart - 将枚举的值反映为类的值
- react-native - 在 redux 商店中保存多个图像是一种好习惯吗?
- vba - 使用 vba 从工作表中分配
- php - 带有 AJAX 请求的帖子表单 - WordPress 插件开发
- ruby-on-rails - 在 rails chartkick 上调用 chartjs 选项
- r - 对R中的哑铃图水平条中的值进行排序
- apache-spark - Spark 数据帧 - 在两个数据帧的联合上使用 groupBy 时避免洗牌
- java - 当我的对象有承包商设置此属性为空时,如何使用 ObjectMapper 将 json 属性的值映射到 Object