我正在尝试从 C# 使用 SOAP 服务,有一个属性定义为
object
但我无法正确传递它。
如果我导入
System.Xml
并创建 XML,则在传递 XMLDoc.OuterString
并解析发送到 SOAP 的 XML 时,它会将 xsi:type
属性添加到 XML 标记,并返回验证错误。
如果我使用一个对象并直接传递它,它会告诉我它无法正确序列化。如果我序列化该对象并将 XML 作为字符串传递,我会再次收到验证错误。
使用 System.XML:
XmlDocument xmlDoc = new XmlDocument();
XmlNode nameNode = xmlDoc.CreateElement("Name");
XmlNode genderNode = xmlDoc.CreateElement("Gender");
genderNode.InnerText = "M";
nameNode.AppendChild(genderNode);
xmlDoc.AppendChild(nameNode);
//this is object item defined on wsdl.
p_SolicitudTransmision[0].Item = xmlDoc.OuterXml; //the value is <name><gender>M</gender></name>
(这个字符串值是可以的,但是当我在Microsoft Service Trace Viewer中分析请求时,该值是:
<data **xsi:type="xsd:string"**>
<name>
<gender>M</gender>
</name>
</data>
我需要删除此
xsi
类型,因为当我尝试在没有此类型的 SOAPUI 上使用 Web 服务时,它可以工作。
我在发起请求时收到此信息。
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Header/>
<soapenv:Body>
<soapenv:Fault>
<faultcode xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">soap-env:Server</faultcode>
<faultstring>Validation error</faultstring>
<detail>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">UndeclaredPrefix: Cannot resolve 'xsd:string' as a QName: the prefix 'xsd' is not declared.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-elt.4.1: The value 'xsd:string' of attribute 'http://www.w3.org/2001/XMLSchema-instance,type' of element 'DatosEspecificos' is not a valid QName.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">UndeclaredPrefix: Cannot resolve 'xsd:string' as a QName: the prefix 'xsd' is not declared.</spring-ws:ValidationError>
<spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-attribute.3: The value 'xsd:string' of attribute 'xsi:type' on element 'DatosEspecificos' is not valid with respect to its type, 'QName'.</spring-ws:ValidationError>
</detail>
</soapenv:Fault>
</soapenv:Body>
</soapenv:Envelope>
最后我添加了一个 MessageInspector 以在发出请求之前停止请求并修改 XML。它可能不是最干净的方法,但是如果不能直接访问模式并且不知道为什么它在分配属性时生成错误的 XML,则很难提供另一种解决方案。