0
我的WCF webservice必須返回xml文件進行下載,因爲我需要序列化來自一個類的對象列表。這裏說的類定義:如何手動序列化對象使用wcf顯示名稱屬性?
[DataContract]
public class ReportItemDriversPerAffiliate : IReportItemDriversPerAffiliate
{
[DataMember(Name = "affiliate_name")]
public string AffiliateName { get; set; }
[DataMember(Name = "affiliate_city")]
public string AffiliateCity { get; set; }
[DataMember(Name = "affiliate_state")]
public string AffiliateState { get; set; }
[DataMember(Name = "drivers_count")]
public int DriversCount { get; set; }
}
而且此功能我用這個代碼:
var totalDriversPerAffiliate = _glManager.GetTotalDriversPerAffiliate<ReportItemDriversPerAffiliate>();
XmlSerializer ser = new XmlSerializer(totalDriversPerAffiliate.GetType());
MemoryStream ms = new MemoryStream();
ser.Serialize(ms, totalDriversPerAffiliate);
ms.Position = 0;
StreamReader r = new StreamReader(ms);
string res = r.ReadToEnd();
var context = System.ServiceModel.Web.WebOperationContext.Current.OutgoingResponse;
context.Headers.Add("Content-Disposition", "attachment; filename=TotalDriversPerAffiliate.xml");
context.ContentType = "application/txt";
byte[] resultBytes = Encoding.UTF8.GetBytes(res);
return new MemoryStream(resultBytes);
但是,在響應文件我在XML領域具有一流的性能,而不是WCF顯示的值的值名稱屬性(有「AffiliateName」,想要「affiliate_name」),我怎麼可以手動序列化這個類,並在xml中的字段像顯示名稱屬性的類?而且,我不想向這個類添加額外的xml屬性,並且我不想在Web服務中創建單獨的方法,它將這些數據作爲xml響應返回。