【发布时间】:2016-03-29 14:04:56
【问题描述】:
我正在尝试针对 WCF 从 SoapUI 执行 SOAP 调用,并且在反序列化 XML 时,它正尝试反序列化为私有字段。为什么会发生这种情况,我该如何解决?代码如下:
我使用标准 XSD.exe 从 XSD 文件生成 POCO 类,结果如下所示:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://iec.ch/TC57/2011/MeterConfig#")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://iec.ch/TC57/2011/MeterConfig#", IsNullable = false)]
public partial class MeterConfig
{
private ComFunction[] comFunctionField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ComFunction")]
public ComFunction[] ComFunction
{
get { return this.comFunctionField; }
set { this.comFunctionField = value; }
}
}
我有一个 WCF SOAP 端点,如下所示:
[ServiceContract]
public class MyApi
{
[OperationContract]
public void CreateMeterConfig2(MeterConfig Payload)
{
//do nothing
}
}
我有一个 SoapUI 测试项目,我在其中提供以下 XML:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
<soapenv:Header/>
<soapenv:Body>
<tem:CreateMeterConfig2>
<tem:Payload>
</tem:Payload>
</tem:CreateMeterConfig2>
</soapenv:Body>
</soapenv:Envelope>
我得到的错误是:
Expecting element 'comFunctionField'
或全部:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:DeserializationFailed</faultcode>
<faultstring xml:lang="en-ZA">The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:Payload. The InnerException message was 'Error in line 13 position 24. 'EndElement' 'Payload' from namespace 'http://tempuri.org/' is not expected. Expecting element 'comFunctionField'.'. Please see InnerException for more details.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>
【问题讨论】: