我一直在敲打它几个小时,对于我的生活,我无法弄清楚如何在WCF中反序列化这个SOAP:
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns:getItemResponse xmlns:ns="http://www.MyApp.com/API">
<return>
<item guid="{EED02630-B742-461C-2F3C-227A2898D40A}">
<name>Widget1</name>
<description>A pretty cool thing</description>
<product>3</product>
<tag1>asdf</tag1>
<tag2>fdsa</tag2>
</item>
</return>
</ns:getItemResponse>
</soapenv:Body>
</soapenv:Envelope>
这是我的服务客户端实现:
[ServiceContract( Namespace = "http://www.MyApp.com/API" )]
public interface ISvc
{
[OperationContract( Action = "getItem" )]
GetItemResponse GetItem( GetItemByNameRequest request );
}
[MessageContract( IsWrapped = true, WrapperName = "getItem" )]
public class GetItemByNameRequest
{
[MessageBodyMember( Name = "name", Namespace = "" )]
public string Name { get; set; }
}
[MessageContract( IsWrapped = true, WrapperName = "getItemResponse", WrapperNamespace = "http://www.MyApp.com/API" )]
public class GetItemResponse
{
[MessageBodyMember( Name = "return", Namespace = "" )]
public Item[ ] Return { get; set; }
}
[DataContract( Name = "item" )]
public class Item
{
[DataMember]
public string name { get; set; }
[DataMember]
public string description { get; set; }
[DataMember]
public string product { get; set; }
}
我想忽略tag1和tag2,所以我没有将它们包含在我的datacontract中(为了简洁起见,我删除的SOAP响应中大约有30个额外的标记我不关心)。 当我调用GetItem方法时,对于上面给出的响应,GetItemResponse.Return的值为null。 我错过了什么?
我会很高兴能够将项目的元素反序列化,但是如果我也可以获得属性,我会更高兴。
我尝试将GetItemResponse类的Return属性更改为XmlNode [],然后从中获取单节点数组,所以我感觉我非常接近。
自动生成的服务引用是不可能的,因为该服务是巨大的并且导致我们的程序集大于10MB并且支付> 1分钟的启动惩罚。它还将结果公开为object[]
而不是Item[]
,这不是非常有用,因为我无法将object[]
转换为Item[]
(实际的基础类型似乎是XmlNode[]
)。
这是响应对象的XSD:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema targetNamespace="http://www.MyApp.com/API" xmlns:myapi="http://www.MyApp.com/API" xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified" attributeFormDefault="unqualified">
<xsd:complexType name="Response" abstract="true">
<xsd:attribute name="sequence" type="xsd:unsignedLong" use="optional" />
</xsd:complexType>
<xsd:complexType name='Item'>
<xsd:sequence minOccurs='0'>
<xsd:element maxOccurs='1' name='name' minOccurs='0' type='xsd:string'></xsd:element>
<xsd:element maxOccurs='1' name='description' minOccurs='0' type='xsd:string'></xsd:element>
<xsd:element maxOccurs='1' name='product' minOccurs='0' type='xsd:int'></xsd:element>
<xsd:element maxOccurs='1' name='tag1' minOccurs='0' type='xsd:string'></xsd:element>
<xsd:element maxOccurs='1' name='tag2' minOccurs='0' type='xsd:string'></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name='getItemResponse' type='myapi:GetItemRes'></xsd:element>
<xsd:complexType name='GetItemRes'>
<xsd:complexContent>
<xsd:extension base='myapi:Response'>
<xsd:sequence>
<xsd:element name='return'>
<xsd:complexType>
<xsd:sequence>
<xsd:element name='item' type='myapi:Item'></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>