私の 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 に対して有効になります。これで私を助けてもらえますか?
ps。
これは、装飾が必要な古い .asmx Web サービスです。wcf を使用してこのサービスを作成することで、この問題を解決できますか?
アイデアをありがとう。