我正在尝试使用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'。
答案 0 :(得分:0)
在xml模式中定义特定类型时:
<serviceResponse xsi:type="out:GetAccountInfoResponse">
您必须以这种方式在 serviceResponse 类的XmlTypeAttribute中指定它
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://AccountService",TypeName = "GetAccountInfoResponse")]