public class Group
{
public string GroupName;
[XmlIgnore]
public string Comments;
}
When serializing, the result is as follows
<?xml version= "1.0" encoding= "Utf-8"?>
<group xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd= "Http://www.w3.org/2001/XMLSchema" >
<GroupName>.Net</GroupName>
</Group>
As we can see, the comments is not serialized because XmlIgnore is set. For properties that already have XmlIgnore set, it is also possible to re-serialize the first serialization by XmlAttributeOverrides.
Public XmlSerializer Createoverrider ()
{
XmlAttributeOverrides Overr = new XmlAttributeOverrides ();
XmlAttributes attrs = new XmlAttributes ();
Attrs. XmlIgnore = true;
Overr. Add (typeof (Group), "Comments", attrs);
Attrs = new XmlAttributes ();
Attrs. XmlIgnore = false;
Overr. Add (typeof (Group), "GroupName", attrs);
XmlSerializer Serilizer = new XmlSerializer ( typeof (Group), overr);
return serilizer;
}
public void SerializeObject (string filename)
{
XmlSerializer Serializer = Createoverrider ();
Group group = new Group ();
Group. GroupName = ". Net";
group.comments = "Microsoft. NET 3.5";
StreamWriter writer = new StreamWriter (filename);
Serializer. Serialize (writer, group);
Writer. Close ();
}
The result of this code, in contrast to the above, serializes the comments and does not contain groupname.
XML attribute overloading