[英]How to serialize properties with DefaultValueAttribute using XmlSerializer?
[英]How do I Selectively Serialize Fields and Properties using XmlSerializer
問題是我有一個測試類和一個 TestVariable,我想在不序列化 TestVariable 的情況下序列化測試類。:
public class TestClass
{
public int TestVariable
{
get;
set;
}
public int ControlVariable
{
get;
set;
}
public TestClass()
{
TestVariable = 1000;
ControlVariable = 9999;
}
}
執行序列化的代碼:
public static void PrintClass()
{
new XmlSerializer(typeof(TestClass)).Serialize(Console.Out, new TestClass());
}
包括命名空間 System.Xml.Serialization 並在要在序列化中排除的字段或屬性上添加屬性 [XmlIgnore]。
修改上面的代碼,它看起來像這樣:
public class TestClass
{
[XmlIgnore]
public int TestVariable
{
get;
set;
}
public int ControlVariable
{
get;
set;
}
public TestClass()
{
TestVariable = 1000;
ControlVariable = 9999;
}
}
這將導致 TestVariable 被完全排除在序列化之外。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.