我使用XmlSerializer
代替DataContractSerializer
在我的ASP.NET Web API項目,並定義爲串行化的ASP.NET Web API
響應對象返回對象
public class MyResponse
{
public string Name {get;set;}
public CustomField<string> Username {get;set;}
public CustomField<float?> Score {get;set;}
}
自定義字段
public class CustomField<T>
{
public T Value {get;set;}
public long LastModified {get;set;}
}
我想生成一個XML響應爲
<MyResponse>
<FirstName>ABC</FirstName>
<Username lastModified="1234">XYZ</Username>
<Score lastModified="45678">12002</Score>
</MyResponse>
的ASP.NET Web API返回JSON對象(我知道,發生這種情況時XmlSerialization無法正常工作),當我裝飾CustomField
類作爲
public class CustomField<T>
{
[XmlText]
public T Value {get;set;}
[XmlAttribute]
public long LastModified {get;set;}
}
我怎樣才能得到想要的XML響應?
這可能會幫助您找出問題http://www.bizcoder.com/index.php/2012/08/10/troubleshooting-serialization-problems-in-web-api/ –
只需檢查,你有您的XML請求中包含Accept標頭?如果請求沒有請求特定的內容類型,則JSON是默認格式。 –
是的,先生。我確實有正確的標題 – frictionlesspulley