DataContractSerializer

DataContractSerializer

本文介绍了应该序列化模式和DataContractSerializer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过 DataContractSerializer 使用 ShouldSerialize * 模式?

Is there a way to get the ShouldSerialize* pattern working with DataContractSerializer?

这是一个小例子:

我有一个简单的类 Person 像这样:

I have a simple class Person which looks like this:

[DataContract]
public class Person
{
  [DataMember]
  public string FirstName { get; set; }
  public bool ShouldSerializeFirstName()
  {
    return !string.IsNullOrEmpty(FirstName);
  }

  [DataMember]
  public string LastName { get; set; }
  public bool ShouldSerializeLastName()
  {
    return !string.IsNullOrEmpty(LastName);
  }

  public Person(string firstName, string lastName)
  {
    FirstName = firstName;
    LastName = lastName;
  }
  public Person(string firstName)
  {
    FirstName = firstName;
  }
  public Person()
  {
  }
}


仅当

FirstName LastName 为空时,才应对其进行序列化。这适用于 XmlSerializer ,但 DataContractSerializer 似乎忽略了 ShouldSerialize 模式。 *指定的模式也不起作用。

FirstName or LastName should only be serialized if they are not null or empty. This works with XmlSerializer but DataContractSerializer seems to ignore the ShouldSerializepattern. The *Specified pattern also doesn't work.

我正在创建两个不同的Xml文件。一个使用DataContractSerializer,一个使用XmlSerializer:

I'm creating two different Xml files. One with DataContractSerializer, one with XmlSerializer:

List<Person> persons = new List<Person>();
persons.Add (new Person("John", "Doe"));
persons.Add (new Person("Carl"));

DataContractSerializer serializer = new DataContractSerializer (typeof (List<Person>));
using (XmlWriter writer = XmlWriter.Create(@"c:\test1.xml", settings))
{
  serializer.WriteObject (writer, persons);
}

XmlSerializer xmlSerializer = new XmlSerializer (typeof (List<Person>));
XmlWriter xmlWriter = XmlWriter.Create (@"c:\text2.xml", settings);
xmlSerializer.Serialize (xmlWriter, persons);
xmlWriter.Close();

文件的输出 test1.xml (DataContractSerializer)看起来像这样:

The output of the file test1.xml (DataContractSerializer) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/XmlSerialization">
  <Person>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
  </Person>
  <Person>
    <FirstName>Carl</FirstName>
    <LastName i:nil="true" />
  </Person>
</ArrayOfPerson>

文件 test2.xml 的输出XmlSerializer)看起来像这样:

The output of file test2.xml (XmlSerializer) looks like this:

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Person>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
  </Person>
  <Person>
    <FirstName>Carl</FirstName>
  </Person>
</ArrayOfPerson>


推荐答案

AFAIK,ShouldSerialize *不适用于datacontract序列化程序。在凯文答案中这是没有用的。您可以删除它。
不幸的是,给定的代码仅在处理空值时才有效。

AFAIK, ShouldSerialize* does not work with datacontract serializer. It is useless in the Kevin answer. You can remove it.Unfortunatly, the code given only work if you handle null value.

这里是一个更通用的解决方案:根据给定条件返回空值。 / p>

Here is a more generic solution: It returns null value depending of a given condition.

    [DataContract]
    public class Person
    {
      private string firstName;
      [DataMember(IsRequired = false, EmitDefaultValue = false)]
      public string FirstName
      {
        get
        {
            //Put here any condition for serializing
            return string.IsNullOrWhiteSpace(firstName) ? null : firstName;
        }
        set
        {
            firstName = value;
        }
      }
    }

这篇关于应该序列化模式和DataContractSerializer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 18:30