假設我有一個customer
類,我會將該類序列化爲xml
。序列化後,我們將得到xml數據,但我需要一些customer
類的屬性根據需要序列化幾個條件。可能嗎?XML序列化相關問題和c#
我沒有概念如何做到這一點。誰能幫我這個?
假設我有一個customer
類,我會將該類序列化爲xml
。序列化後,我們將得到xml數據,但我需要一些customer
類的屬性根據需要序列化幾個條件。可能嗎?XML序列化相關問題和c#
我沒有概念如何做到這一點。誰能幫我這個?
您可以添加一個或多個ShouldSerializeXXXXXX()
方法,其中XXXXXX
是你要根據條件連載每個屬性的名稱。
例如爲:
public class Customer
{
[DefaultValue(null)]
public string SomeInfo { get; set; }
[DefaultValue(null)]
public string SomeOtherInfo { get; set; }
#region Serialization conditions
// should SomeInfo be serialized?
public bool ShouldSerializeSomeInfo()
{
return SomeInfo != null; // serialize if not null
}
// should SomeOtherInfo be serialized?
public bool ShouldSerializeSomeOtherInfo()
{
return SomeOtherInfo != null; // serialize if not null
}
#endregion
}
您可以使用XmlAttributeOverrides
並覆蓋屬性的XmlIgnore
屬性。
(沒有在XmlIgnore
MSDN頁面爲例)
看看this post on SO。可以對你有所幫助。