除了字典之外,我的 Web 服务没有我以这种方式序列化的任何其他属性。
public class DictionarySerializer : IXmlSerializable
{
private IDictionary dictionary;
public DictionarySerializer()
{
this.dictionary = new Hashtable();
}
public DictionarySerializer(IDictionary dictionary)
{
this.dictionary = dictionary;
}
public void addToDict(object key, object value)
{
this.dictionary.Add(key, value);
}
public static void Serialize(IDictionary dictionary, Stream stream)
{
DictionarySerializer ds = new DictionarySerializer(dictionary);
XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
xs.Serialize(stream, ds);
}
public static IDictionary Deserialize(Stream stream)
{
XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
DictionarySerializer ds = (DictionarySerializer)xs.Deserialize(stream);
return ds.dictionary;
}
XmlSchema IXmlSerializable.GetSchema()
{
return (null);
}
void IXmlSerializable.ReadXml(XmlReader reader)
{
reader.Read();
reader.ReadStartElement("dictionary");
while (reader.NodeType != XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
string key = reader.ReadElementString("key");
string value = reader.ReadElementString("value");
reader.ReadEndElement();
reader.MoveToContent();
dictionary.Add(key, value);
}
reader.ReadEndElement();
}
void IXmlSerializable.WriteXml(XmlWriter writer)
{
foreach (object key in dictionary.Keys)
{
object value = dictionary[key];
writer.WriteStartElement(key.ToString());
writer.WriteValue(value.ToString()); //WriteElementString("value", value.ToString());
writer.WriteEndElement();
}
}
}
在我的 WS 中,我以这种方式动态地用属性填充我的字典
this.DICTIONARY.addToDict("WARRANTY", drv["Warranty"].ToString());
this.DICTIONARY.addToDict("IMAGEURL", drv["ImageUrl"].ToString());
this.DICTIONARY.addToDict("RETAILPRICE", drv["RetailPrice"].ToString());
并且我能够使用包含此字典元素的 SoapUI 获得响应:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetProductsResponse xmlns="http://www.xxx.com/B2B">
<GetProductsResult>
<PRODUCT>
<DICTIONARY>
<IMAGE_URL>888009612_1.jpg</IMAGE_URL>
<WARRANTY>1G</WARRANTY>
<RETAILPRICE>0.00000000000000000000</RETAILPRICE>
</DICTIONARY>
</PRODUCT>
</GetProductsResult>
</GetProductsResponse>
</soap:Body>
</soap:Envelope>
问题是响应对 wsdl 模式无效!这是我描述字典的 wsdl 的一部分:
<s:element name="GetProductsResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetProductsResult" type="tns:ArrayOfPRODUCT" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfPRODUCT">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="PRODUCT" nillable="true" type="tns:PRODUCT" />
</s:sequence>
</s:complexType>
<s:complexType name="PRODUCT">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="DICTIONARY">
<s:complexType>
<s:sequence>
<s:element ref="s:schema" />
<s:any />
</s:sequence>
</s:complexType>
</s:element>
</s:sequence>
</s:complexType>
我在 SoapUI 中遇到的错误是这样的
Expected element 'schema@http://www.w3.org/2001/XMLSchema' instead of 'WARRANTY@http://www.xxx.com/B2B' here in element DICTIONARY@http://www.xxx.com/B2B
Expected element 'schema@http://www.w3.org/2001/XMLSchema' instead of 'RETAILPRICE@http://www.xxx.com/B2B' here in element DICTIONARY@http://www.xxx.com/B2B
Expected element 'schema@http://www.w3.org/2001/XMLSchema' before the end of the content in element DICTIONARY@http://www.xxx.com/B2B
这让我很困惑。我不知道这个错误是什么意思以及如何解决这个问题,以便我的响应对 wsdl 有效。你能帮我解决这个问题吗???
附言。
这是一个旧的 .asmx Web 服务,需要一些修饰。我是否要使用 wcf 编写此服务来解决此问题?
感谢您的任何想法。