我正在編寫使用XmlSerializer的WCF服務。該服務是一個基於WSDL和一個XSD的實現,這個XSD是從將要使用該服務的組提供給我的。基本上它是他們的系統和我的系統之間的數據適配器。爲數組屬性指定xsi:type
一個特定的類有一個屬性,該屬性是項目中定義的另一個引用類型的數組。我需要爲此屬性指定xsi:type
。
我使用svcutil從WSDL和XSD生成代碼,然後在幾個地方「固定」生成的代碼。這個屬性是我必須修復的。
的類定義是(修剪下降到只有問題區域):
[GeneratedCode("svcutil", "4.6.1055.0")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(Namespace = "urn:example.com:types")]
public class userData
{
private ItemType[] itemTypeField;
[XmlArray(Order = 0, Namespace = "urn:example.com:types")]
[XmlArrayItem("item", IsNullable = false, Type = typeof(ItemType), Namespace = "urn:example.com:types")]
public ItemType[] myprop
{
get { return itemTypeField; }
set { itemTypeField = value; }
}
}
被調用服務的方法時產生的XML是(userData
是getUserResponse
類的屬性):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<getUserResponse xmlns="urn:example.com:types">
<userData>
<myprop>
<item>
<key>some key</key>
<value>some value</value>
</item>
<item>
<key>some other key</key>
<value>some other value</value>
</item>
</myprop>
</userData>
</getUserResponse>
</s:Body>
</s:Envelope>
我需要<myprop>
元素看起來像這樣:
<myprop xsi:type="ns1:MapType"
xmlns:ns1="urn:example.com:types"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
如何獲取XmlSerializer爲陣列屬性myprop
生成xsi:type
?
我發現這些類似的問題作爲最接近我的問題,但他們並不適用於數組:
xsi type and xsd tag
How can I force the use of an xsi:type attribute?
我需要用自定義序列做到這一點?
請讓我知道,如果我需要張貼任何服務合同。服務中的其他一切都運行良好,只是這一點點而已。