If you need more control over the serialization, you can decorate the class with the DataContract attribute. When this attribute is present, the class is serialized as follows:
如果需要在序列化上的更多控制,可以用DataContract注解属性修饰类。当这个注解属性出现时,该类按以策略序列化:
“Opt in(选入)”方法:属性与字段默认不被序列化。为了序列化一个属性或字段,请用DataMember注解属性修饰它。
要序列化private或protected成员,请用DataMember注解属性修饰它。
只读属性不被序列化。
要改变类名在XML中的呈现,请在DataContract注解属性中设置Name参数。
要改变成员名在XML中的呈现,请设置DataMember注解属性中的Nmae参数
要改变XML命名空间,请设置DataContract类中的Namespace参数。
只读属性
Read-only properties are not serialized. If a read-only property has a backing private field, you can mark the private field with the DataMember attribute. This approach requires the DataContract attribute on the class.
只读属性是不被序列化的。如果只读属性有一个支撑private字段,可以用DataMember注解属性对这个private字段进行标记。这种办法需要在类上使用DataContract注解属性。
[DataContract]
public class Product
{
[DataMember]
private int pcode; // serialized(序列化的)
// Not serialized (read-only)
// 不作序列化(只读)
public int ProductCode { get { return pcode; } }
}
Dates are written in ISO 8601 format. For example, "2012-05-23T20:21:37.9116538Z".
日期被写成ISO 8601格式。例如,“2012-05-23T20:21:37.9116538Z”。
To write indented XML, set the Indent property to true:
要书写缩进的XML,请将Indent属性设置为true:
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; xml.Indent = true;
Setting Per-Type XML Serializers设置每一类型(Per-Type)的XML序列化器
You can set different XML serializers for different CLR types. For example, you might have a particular data object that requires XmlSerializer for backward compatibility. You can use XmlSerializer for this object and continue to use DataContractSerializer for other types.
你可以为不同的CLR类型设置不同的XML序列化器。例如,你可能有一个特殊的数据对象,它出于向后兼容而需要XmlSerializer。你可以为此对象使用XmlSerializer,而对其它类型继续使用DataContractSerializer。
To set an XML serializer for a particular type, call SetSerializer.
为了设置用于特殊类型的XML序列化器,要调用SetSerializer。
var xml = GlobalConfiguration.Configuration.Formatters.XmlFormatter; // Use XmlSerializer for instances of type "Product": // 对“Product”类型的实例使用XmlSerializer: xml.SetSerializer<Product>(new XmlSerializer(typeof(Product)));
You can specify an XmlSerializer or any object that derives from XmlObjectSerializer.
你可以指定一个XmlSerializer,或任何派生于XmlObjectSerializer的对象。
6.2.5 去除JSON或XML格式化器
You can remove the JSON formatter or the XML formatter from the list of formatters, if you do not want to use them. The main reasons to do this are:
你可以从格式化器列表中删除JSON格式化器,或XML格式化器,只要你不想使用它们。这么做的主要原因是:
将你的Web API响应限制到特定的媒体类型。例如,你可能决定只支持JSON响应,而删除XML格式化器。
用一个自定义格式化器代替默认的格式化器。例如,你可能要用自己的自定义JSON格式化器实现来代替(默认的)JSON格式化器。
The following code shows how to remove the default formatters. Call this from your Application_Start method, defined in Global.asax.
以下代码演示了如何删除默认的格式化器。在Global.asax中定义的Application_Start方法中调用它。
void ConfigureApi(HttpConfiguration config)
{
// Remove the JSON formatter
// 删除JSON格式化器
config.Formatters.Remove(config.Formatters.JsonFormatter);
// or(或者)
// Remove the XML formatter
// 删除XML格式化器
config.Formatters.Remove(config.Formatters.XmlFormatter);
}
6.2.6 处理循环对象引用