0

希望序列化成为“选择加入”过程,以便只有具有[DataMember]属性的字段显示在 xml 中。

看了下DataContractSerializerSettings,好像什么都没有。在下面尝试踢腿和咯咯笑,但 xml 仍然包含没有属性的[DataMember]属性......

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

2 回答 2

1

在 .NET 3.5 SP1 中,使用 [ ] 修饰的成员DataMember,任何公共读/写属性都将由DataContractSerializer. 为了选择退出,您必须与私人会员一起去。DataMemeber在 .NET 3.5 之前情况并非如此,只有用 [ ]装饰的成员才会被序列化。

详细了解 .NET 3.5 SP1 博客中的新增功能。

于 2013-11-10T17:56:16.177 回答
0


,那不是真的,请尝试查看 msdn 中的此代码,并尝试取消注释并注释类 person 中属性 LastName 的 datamember 属性,例如,一个 yot 会看到它没有像@Nair saied 那样序列化。我认为您正在尝试写入同一个文件,但该文件已被锁定,因此您会认为所有属性都已序列化。尝试每次更改名称,您会看到未使用 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));
    }

}
于 2013-11-10T19:07:36.877 回答