简体   繁体   English

RC更新后的ASP.NET WebAPI XML序列化

[英]ASP.NET WebAPI XML Serialization after RC update

Prior to the asp.net 4 RC update, I had been using the WebApi for outputting XML. 在asp.net 4 RC更新之前,我一直在使用WebApi来输出XML。 My model had XmlSerialization attributes [XmlElement(ElementName = "the name")] so that I could use friendlier names. 我的模型有XmlSerialization属性[XmlElement(ElementName = "the name")]所以我可以使用更友好的名字。

eg 例如

[XmlElement(ElementName = "Branch")]
public string site_nm { get; set; }

After the .net 4 RC update, the XML which is output ignores this. 在.net 4 RC更新之后,输出的XML忽略了这一点。

I know that some things have changed in the WebApi, like to make a method return values based on OData protocols when returning IQueryable<T> , you now need to add [Queryable] to your methods. 我知道WebApi中有些东西已经改变,比如在返回IQueryable<T>时使方法返回基于OData协议的值,你现在需要在方法中添加[Queryable]

Is there something that I need to add to make the serialization work? 我需要添加一些东西才能使序列化工作吗? I have read through change notes but can't see anything. 我已阅读更改说明,但看​​不到任何内容。

You need to instruct the formatter to use XmlSerializer: 您需要指示格式化程序使用XmlSerializer:

GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSerializer = true;

The default now is the DataContractSerializer. 现在默认是DataContractSerializer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM