Thoughts from Dan Miser RSS 2.0
 Monday, January 08, 2007
While the XmlIgnore/Specified pattern to control serialization is very useful, it could be overkill for simpler serialization scenarios.

When you create an object with a bool field, that field is initialized and defaulted to the value of false. Under normal conditions, this value will get serialized every time. However, what if we only care about capturing that value when the value is true? It turns out that we can use DefaultValueAttribute. The first thing that may stand out to you is that this attribute lives in the System.ComponentModel namespace, and not in an XML namespace. The online help also mentions that this is used for visual designers and code generators. XmlSerializer also uses this attribute to decide whether or not to stream the value during serialization, since it's really a form of a code generator (more on that in a later post). If the value of the field or property matches the value that you specify in the DefaultValueAttribute, then that property will not be streamed.

Using an XmlAttributeAttribute, you can specify that a certain property will be expressed as an XML attribute instead of an XML element. To demonstrate both of these concepts, see the following code:

public class Url

{

    [XmlAttribute("address")]

    public string Address;

    [XmlAttribute("primary"), DefaultValue(false)]

    public bool Primary;

    public Url()

    {

   }

}

static void Main(string[] args)

{

    Url u = new Url();

    u.Address = "http://testing/";

    string xml = GetXMLFromObject(u);

    Console.WriteLine(xml);

}

This produces roughly the following output (XML namespace information is stripped here. I will show how to do this in code with a future post). Notice that the Primary attribute is not generated because it matches the DefaultValue.

<?xml version="1.0" encoding="utf-16"?>

<Url address="http://testing/" />

Monday, January 08, 2007 9:09:43 AM (Central Standard Time, UTC-06:00)  #    Comments [0] -
.NET | XML
Tracked by:
http://9na-information.info/30104141/quality-of-life-on-teh-urban-fringe.html [Pingback]
http://9nc-information.info/17731578/index.html [Pingback]
http://9nn-information.info/29385313/washington-international-horse-show.html [Pingback]
http://9nr-information.info/24444684/institute-for-publci-health-informatics-cdc... [Pingback]
http://9nd-information.info/91323954/index.html [Pingback]
http://9ne-information.info/93972562/icon-motorcycle-helmets.html [Pingback]
http://9nm-information.info/05213210/index.html [Pingback]
http://9nv-information.info/32244140/index.html [Pingback]
http://9nf-information.info/64694721/index.html [Pingback]
http://9nl-information.info/53522691/index.html [Pingback]
http://9ne-information.info/24443951/index.html [Pingback]
http://9oj-information.info/01325858/index.html [Pingback]
http://9ot-information.info/00667373/index.html [Pingback]
http://9oh-information.info/40847082/index.html [Pingback]
http://9oj-information.info/74240998/brandon-gilliam-music.html [Pingback]
http://9qj-information.info/09633343/grossista-oggettistica-ferrara.html [Pingback]
http://9oi-information.info/27723334/travel-package-sanibel-florida.html [Pingback]
http://9ov-information.info/08764184/index.html [Pingback]
http://9rm-information.info/61741823/index.html [Pingback]
http://9sl-information.info/44626791/index.html [Pingback]
http://9rw-information.info/88501827/index.html [Pingback]
http://9rc-information.info/51517891/index.html [Pingback]
http://9uaef-le-informazioni.info/91327911/index.html [Pingback]
http://9uafr-le-informazioni.info/44968315/index.html [Pingback]
http://9uafr-le-informazioni.info/45426125/test-for-echo.html [Pingback]
http://9uafh-le-informazioni.info/69454371/ssis-unige.html [Pingback]
http://9uafi-le-informazioni.info/49957476/index.html [Pingback]
http://9uafl-le-informazioni.info/71907392/index.html [Pingback]
http://9uaeh-le-informazioni.info/89255644/index.html [Pingback]
http://9uafd-le-informazioni.info/80004463/index.html [Pingback]
http://9uaec-le-informazioni.info/57641435/index.html [Pingback]
http://9uafa-le-informazioni.info/00826748/index.html [Pingback]
http://9uahd-le-informazioni.info/70429601/rete-doghe-legno-struttura-legno.html [Pingback]
http://9uagl-le-informazioni.info/25179444/index.html [Pingback]
http://9uago-le-informazioni.info/04670905/occhio-rosso-it.html [Pingback]
http://9uagp-le-informazioni.info/85519868/index.html [Pingback]
http://9uagb-le-informazioni.info/41114844/crash-test-alfa.html [Pingback]
http://9uahm-le-informazioni.info/78996257/index.html [Pingback]
http://9uagm-le-informazioni.info/99708418/index.html [Pingback]
http://9uagm-le-informazioni.info/32336611/canon-eos-1-ds.html [Pingback]
Navigation
Archive
<November 2008>
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
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 2008
Dan Miser
Sign In
Statistics
Total Posts: 310
This Year: 25
This Month: 0
This Week: 0
Comments: 605
All Content © 2008, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)