我的WCF服务方法:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "decl/xml?s={s}")]
public Paradigm GetDeclensionXml(string s)
{
return GetDeclension (s);
}
返回一个包含一串字符串字段的自定义对象:
public class Paradigm
{
public string genitive;
public string dative;
public string accusative;
public string instrumental;
public string prepositional;
}
被序列化为此XML:
<Paradigm xmlns="http://schemas.datacontract.org/2004/07/MorpherWebDemo20.ws" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<accusative>росу</accusative>
<dative>росе</dative>
<genitive>росы</genitive>
<instrumental>росой</instrumental>
<prepositional>росе</prepositional>
</Paradigm>
即。标签按名称自动排序。我希望它们按照我在custon类中定义的顺序出现。任何想法如何实现?
P.S。如果我将ResponseFormat更改为JSON,也会发生同样的情况。
答案 0 :(得分:1)
您可以使用[DataContract]
和[DataMember]
成员装饰该类,并且您将能够使用此属性的Order
属性来执行您想要的操作:
[DataContract]
public class Paradigm
{
[DataMember(Order = 1)]
public string genitive;
[DataMember(Order = 2)]
public string dative;
[DataMember(Order = 3)]
public string accusative;
[DataMember(Order = 4)]
public string instrumental;
[DataMember(Order = 5)]
public string prepositional;
}