将ISerializable与DataContractSerializer一起使用时,如何停止序列化程序输出类型信息? [英] When using ISerializable with DataContractSerializer, how do I stop the serializer from outputting type information?

查看:46
本文介绍了将ISerializable与DataContractSerializer一起使用时,如何停止序列化程序输出类型信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地控制序列化,我将一个类从 [DataContract] 转换为 [Serializable] ,同时实现了 GetObjectData 和特殊的反序列化构造函数.当我这样做时,发出的XML现在将类型信息应用于所有元素.我不需要这些多余的信息,我想知道如何通知串行器不输出它.

To get more control over serialization, I have converted a class from [DataContract] to [Serializable], implementing both GetObjectData and the special deserializing constructor. When I do this, the XML emitted now has type information applied to all elements. I don't want this superfluous information, and I'm wondering how to inform the serializer to not output it.

以下是使用 [DataContract] 的示例代码:

Here's the sample code that uses [DataContract]:

[DataContract(Namespace = "")]
class Test 
{
    public Test() { }
    [DataMember]
    public Nullable<int> NullableNumber = 7;
    [DataMember]
    public int Number = 5;

    public static void Go()
    {
        var test = new Test();
        var dcs = new DataContractSerializer(typeof(Test));
        using (var s = new StreamWriter("test.xml"))
        {
            dcs.WriteObject(s.BaseStream, test);
        }
    }        
}

这将输出以下XML(请注意,在Nullable Number和Number上没有类型信息,这是所需的输出):

This outputs the following XML (notice no type info on Nullable Number and Number--this is the desired output):

<Test xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <NullableNumber>7</NullableNumber>
  <Number>5</Number>
</Test>

如果我按如下方式修改上面的代码(添加[Serializable] 、: ISerializable和两个序列化方法):

If I modify the above code as follows (adding [Serializable], : ISerializable, and the two serialization methods):

[Serializable]
class Test : ISerializable
{
    public Test() { }
    public Nullable<int> NullableNumber = 7;
    public int Number = 5;

    public static void Go()
    {
        var test = new Test();
        var dcs = new DataContractSerializer(typeof(Test));
        using (var s = new StreamWriter("test.xml"))
        {
            dcs.WriteObject(s.BaseStream, test);
        }
    }        
    public Test(SerializationInfo info, StreamingContext context)
    {
        NullableNumber = info.GetInt32("NullableNumber");
        Number = info.GetInt32("Number");
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("NullableNumber", NullableNumber);
        info.AddValue("Number", Number);
    }
}

现在它发出以下XML.注意添加到每个元素的类型信息(i:type ="x:int").

It now emits the following XML. Notice the type information (i:type="x:int") added to each element.

<Test xmlns="http://schemas.datacontract.org/2004/07/XMLSerialization" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="http://www.w3.org/2001/XMLSchema">
  <NullableNumber i:type="x:int" xmlns="">7</NullableNumber>
  <Number i:type="x:int" xmlns="">5</Number>
</Test>

为什么要这样做?如何停止执行此操作?

Why is it doing this? How do I stop it from doing it?

谢谢!

推荐答案

从.Net Framework 4.5(和.Net Core 1.0)开始,可以使用 DataContractJsonSerializerSettings 类:

Starting in .Net Framework 4.5 (and .Net Core 1.0), this is possible using the DataContractJsonSerializerSettings class:

DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings
{
    EmitTypeInformation = EmitTypeInformation.Never
};
var dcs = new DataContractSerializer(typeof(Test), settings);

EmitTypeInformation 设置告诉序列化程序在序列化过程中不要输出(烦人?) __ type 参数.

The EmitTypeInformation settings tells the serializer not to output the (annoying?) __type parameter during serialization.

还有一系列其他有用的设置.这是文档页面 DataContractJsonSerializerSettings .

There are a range of other useful settings available. Here is the docs page for DataContractJsonSerializerSettings.

这篇关于将ISerializable与DataContractSerializer一起使用时,如何停止序列化程序输出类型信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆