The problem is I have a test class and a TestVariable and I would like to Serialize the Test Class without Serializing the TestVariable.:
public class TestClass
{
public int TestVariable
{
get;
set;
}
public int ControlVariable
{
get;
set;
}
public TestClass()
{
TestVariable = 1000;
ControlVariable = 9999;
}
}
The Code that does the serialization:
public static void PrintClass()
{
new XmlSerializer(typeof(TestClass)).Serialize(Console.Out, new TestClass());
}
Include the Namespace System.Xml.Serialization and adding the attribute [XmlIgnore] over the field or property that you want to be excluded in Serialization.
Modifying the Above code it would look like this:
public class TestClass
{
[XmlIgnore]
public int TestVariable
{
get;
set;
}
public int ControlVariable
{
get;
set;
}
public TestClass()
{
TestVariable = 1000;
ControlVariable = 9999;
}
}
This will cause TestVariable to be completely excluded from the serialization.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.