Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Set XmlSerialize to ignore fields in their default value

Set XmlSerialize to ignore fields in their default value

Scheduled Pinned Locked Moved C#
questionxml
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    I have the below campaign class. It is serialized to xml in my code. However, I do not want it to serialize the field if it's set to it's default value. In my case, 0 and 2001-01-01T00:00:00-08:00 for int's and datetime's respectively. How can I have the XmlSerializer ignore them? Is there a global setting for the XmlSerializer class or attributes I can set for each field?

    public class campaign : RESTResource
    {
        public override String get\_endpoint\_url()
        {
            return campaign.endpoint\_url;
        }
    
        public static string endpoint\_url = "/campaigns/";
    
        public int id; //if 0; don't serialize
        public string name;
        public int category\_id; if 0; don't serialize
        public string subject; 
        public string body; 
        public string thumbnail; 
        public string notes; 
        public int ad\_id\_source; //If 0; don't serialize
        public int campaign\_status\_id; //If 0; don't serialize
        public DateTime valid\_after; //if 2001-01-01T00:00:00-08:00, don't serialize
        public int billing\_code; //If 0; don't serialize
        public DateTime created\_at; //if 2001-01-01T00:00:00-08:00, don't serialize
        public DateTime updated\_at; //if 2001-01-01T00:00:00-08:00, don't serialize
    }
    

    /\ |_ E X E GG

    L 1 Reply Last reply
    0
    • E eggie5

      I have the below campaign class. It is serialized to xml in my code. However, I do not want it to serialize the field if it's set to it's default value. In my case, 0 and 2001-01-01T00:00:00-08:00 for int's and datetime's respectively. How can I have the XmlSerializer ignore them? Is there a global setting for the XmlSerializer class or attributes I can set for each field?

      public class campaign : RESTResource
      {
          public override String get\_endpoint\_url()
          {
              return campaign.endpoint\_url;
          }
      
          public static string endpoint\_url = "/campaigns/";
      
          public int id; //if 0; don't serialize
          public string name;
          public int category\_id; if 0; don't serialize
          public string subject; 
          public string body; 
          public string thumbnail; 
          public string notes; 
          public int ad\_id\_source; //If 0; don't serialize
          public int campaign\_status\_id; //If 0; don't serialize
          public DateTime valid\_after; //if 2001-01-01T00:00:00-08:00, don't serialize
          public int billing\_code; //If 0; don't serialize
          public DateTime created\_at; //if 2001-01-01T00:00:00-08:00, don't serialize
          public DateTime updated\_at; //if 2001-01-01T00:00:00-08:00, don't serialize
      }
      

      /\ |_ E X E GG

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      [XmlIgnore]

      E 1 Reply Last reply
      0
      • L Lost User

        [XmlIgnore]

        E Offline
        E Offline
        eggie5
        wrote on last edited by
        #3

        Doesn't XmlIgnore completely ignore the field without any exception?

        /\ |_ E X E GG

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups