XML attribute overloading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.