默认情况下,DataContractSerializer会被忽略

时间:2013-11-10 17:35:39

标签: c# serialization

希望将seralization设置为“选择加入”流程,以便只在xml中显示具有[DataMember]属性的字段。

查看DataContractSerializerSettings,但似乎没有任何东西。尝试下面的踢和笑,但xml仍然包含没有[DataMember]属性的属性......

  DataContractSerializer writer = 
    new DataContractSerializer( typeof( ProductAAnimals ), 
    new DataContractSerializerSettings()
    {
        IgnoreExtensionDataObject = true
    } );

2 个答案:

答案 0 :(得分:1)

对于使用[DataMember]装饰的.NET 3.5 SP1成员,任何公共读/写属性都将由DataContractSerializer自动序列化。为了选择退出,您必须与私人会员一起。在.NET 3.5之前不是这种情况,只有[DataMemeber]装饰的成员才被序列化。

更多关于.NET 3.5 SP1中的新功能 Blog

答案 1 :(得分:0)

0
这不是真的尝试从msdn看一下这个代码,并尝试取消注释并在类person中注释属性LastName的datamember属性,例如,yot会看到它没有被序列化为@Nair。我认为您正在尝试写入同一个文件,但文件被锁定,因此您会认为所有属性都已序列化。每次尝试更改名称,您将看到未标记为datamember属性的属性将不会被序列化

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

    // You must apply a DataContractAttribute or SerializableAttribute 
// to a class to have it serialized by the DataContractSerializer.
[DataContract()]
class Person : IExtensibleDataObject
{
private string LastNameValue;
// Apply the DataMemberAttribute to fields (or properties)  
// that must be serialized.
[DataMember()]
public string FirstName;

//[DataMember()]
public string LastName
{
    get { return LastNameValue; }
    set { LastNameValue = value; }
}

[DataMember(Name = "ID")]
public int IdNumber;

// Note that you can apply the DataMemberAttribute to  
// a private field as well.
[DataMember]
private string Secret;

public Person(string newfName, string newLName, int newIdNumber)
{
    FirstName = newfName;
    LastName = newLName;
    IdNumber = newIdNumber;
    Secret = newfName + newLName + newIdNumber;
}

// The extensionDataValue field holds data from future versions  
// of the type.  This enables this type to be compatible with  
// future versions. The field is required to implement the  
// IExtensibleDataObject interface. 

private ExtensionDataObject extensionDatavalue;

public ExtensionDataObject ExtensionData
{
    get
    {
        return extensionDatavalue;
    }
    set
    {
        extensionDatavalue = value;
    }
}
}

  public class Test
  {
public static void Main(string[] args)
{
    try
    {
        WriteObject(@"DataMemberAttributeExample.xml");
        ReadObject(@"DataMemberAttributeExample.xml");
    }
    catch (Exception exc)
    {
        Console.WriteLine(
        "The serialization operation failed: {0} StackTrace: {1}",
        exc.Message, exc.StackTrace);
    }
    finally
    {
        Console.WriteLine("Press <Enter> to exit....");
        Console.ReadLine();
    }
}

public static void WriteObject(string filename)
{
    // Create a new instance of the Person class.
    Person p1 = new Person("Zighetti", "Barbara", 101);
    FileStream writer = new FileStream(filename,
    FileMode.OpenOrCreate);
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    ser.WriteObject(writer, p1);
    writer.Close();
}

public static void ReadObject(string filename)
{
    // Deserialize an instance of the Person class  
    // from an XML file.
    FileStream fs = new FileStream(filename,
    FileMode.OpenOrCreate);
    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));
    // Deserialize the data and read it from the instance.
    Person deserializedPerson = (Person)ser.ReadObject(fs);
    fs.Close();
    Console.WriteLine(String.Format("{0} {1}, ID: {2}",
    deserializedPerson.FirstName, deserializedPerson.LastName,
    deserializedPerson.IdNumber));
    }

}