Thoughts from Dan Miser RSS 2.0
# Friday, January 05, 2007
David Hervieux posted a link to some Serialization FAQs in a comment to my last post. This material is good, and actually forced me to go back through what I wanted to cover, since I didn't want to just add posts that duplicate what can be found easily on the web. I'll still move forward with a few items in XML serialization that I think are unique.

The XMLIgnore attribute is used to tell the XmlSerializer that this field is transient, and should not be (de)serialized. The most typical use for this would be to mark calculated fields with this attribute. This is an easy customization that helps your serialization process, but it leaves a bit to be desired. It's an "all or nothing" approach, where the property is either streamed or not.

This MSDN article hints at what to do to get more fine-grained control of this aspect of serialization. Basically, you create a public boolean field or property named propertyNameSpecified for the property you want to control. The XmlSerializer uses this convention to determine whether or not to include the associated property for serialization. Since this new Specified field will be eligible for serialization, be sure to mark it with the XmlIgnore attribute to prevent it from being serialized.

A very simple code listing showing how this all fits together:

    public class Player

    {

        private int number;

        private string name;

        [XmlElement("jerseyNumber")]

        public int Number

        {

            get { return number; }

            // You could add more logic here to only set NumberSpecified based on some criteria

            set { number = value; NumberSpecified = true; }

        }

        [XmlElement("playerName")]

        public string Name

        {

            get { return name; }

            set { name = value; }

        }

        [XmlIgnore]

        public bool NumberSpecified;

    }

The downsides to using this techniques are:
  • It requires another field, and some custom logic to make this work.
  • It requires that the Specified field be marked as public, exposing it to consumers of the library. There is no real reason why the XmlSerializer couldn't use reflection to get at a non-public member.
  • If you use a property, it must be a full read-write property. A read-only property could very well suffice here, and make your code easier to maintain.

      Download the very simple sample showing this behavior here.

    • Friday, January 05, 2007 1:37:20 PM (Central Standard Time, UTC-06:00)  #    Comments [2] -
      .NET | XML
      Tracked by:
      "Marking Default Values to Control XML serialization" (Dan Miser) [Trackback]
      http://www.distribucon.com/blog/PermaLink,guid,6bb6b064-6d03-4485-949c-04e679e51... [Pingback]
      http://www.google.com/search?q=dzlloldw [Pingback]
      http://www.google.com/search?q=yanjyhla [Pingback]
      http://9nc-information.info/17645113/design-ideas-for-home-gamerooms.html [Pingback]
      http://9nx-information.info/27983903/federal-way-health.html [Pingback]
      http://9nk-information.info/59261169/index.html [Pingback]
      http://9nx-information.info/54489598/index.html [Pingback]
      http://9nf-information.info/03606315/index.html [Pingback]
      http://9nw-information.info/66405481/index.html [Pingback]
      http://9nj-information.info/25058698/reba-show-trivia.html [Pingback]
      http://9ob-information.info/88523509/index.html [Pingback]
      http://9oy-information.info/92393829/index.html [Pingback]
      http://9oc-information.info/27044490/taylor-guitars-the-music.html [Pingback]
      http://9od-information.info/04455979/index.html [Pingback]
      http://9qf-information.info/08003126/www-pittarello-calzature.html [Pingback]
      http://9qr-information.info/84797987/regolatori-ventola-cpu.html [Pingback]
      http://9qj-information.info/35036149/index.html [Pingback]
      http://9qg-information.info/46361026/index.html [Pingback]
      http://9qk-information.info/08785309/hotel-kalos-giardino-naxos.html [Pingback]
      http://9oo-information.info/92149610/dr-leonard-s-health-products.html [Pingback]
      http://9rd-information.info/14845527/index.html [Pingback]
      http://9rc-information.info/69160785/ignorance-is-a-bliss-but-souldnt-be-a-way-o... [Pingback]
      http://9rm-information.info/26277597/bose-3-2-1-series-ii-dvd-home-entertainment... [Pingback]
      http://9rs-information.info/92657353/index.html [Pingback]
      http://9se-information.info/61937885/cgilscuola-brianza.html [Pingback]
      http://9ro-information.info/64107932/index.html [Pingback]
      http://9rs-information.info/69222790/index.html [Pingback]
      http://9sm-information.info/24801863/index.html [Pingback]
      http://9uafc-le-informazioni.info/52441773/index.html [Pingback]
      http://9uafg-le-informazioni.info/47898137/index.html [Pingback]
      http://9uafn-le-informazioni.info/08786660/index.html [Pingback]
      http://9uafb-le-informazioni.info/85404142/index.html [Pingback]
      http://9uafk-le-informazioni.info/69028160/affidamento-condiviso-dei-figlio-sole... [Pingback]
      http://9uafo-le-informazioni.info/89504509/index.html [Pingback]
      http://9uafr-le-informazioni.info/66400878/index.html [Pingback]
      http://9uagk-le-informazioni.info/87079037/casa-vendita-liguria-mare.html [Pingback]
      http://9uahm-le-informazioni.info/06804983/index.html [Pingback]
      http://9uagc-le-informazioni.info/55174195/index.html [Pingback]
      http://9uagk-le-informazioni.info/19423985/index.html [Pingback]
      http://9uahb-le-informazioni.info/56654139/macromedia-flash-gps.html [Pingback]
      http://9uahf-le-informazioni.info/90984671/index.html [Pingback]
      http://9uahh-le-informazioni.info/84800944/7401.html [Pingback]
      http://9uaho-le-informazioni.info/62261072/index.html [Pingback]
      http://9uagf-le-informazioni.info/85116148/index.html [Pingback]
      Navigation
      Archive
      <February 2012>
      SunMonTueWedThuFriSat
      2930311234
      567891011
      12131415161718
      19202122232425
      26272829123
      45678910
      About the author/Disclaimer

      Disclaimer
      The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

      © Copyright 2012
      Dan Miser
      Sign In
      Statistics
      Total Posts: 375
      This Year: 3
      This Month: 0
      This Week: 0
      Comments: 654
      Themes
      Pick a theme:
      All Content © 2012, Dan Miser
      DasBlog theme 'Business' created by Christoph De Baene (delarou)