My reasoning as to why it works with string and not DateTime is that string
is a reference type, while DateTime is a value type. If the string reference
is null, the serialization engine doesn't bother to serialize the structure
(attribute in this case). This contradicts your point with respect to int's
though; Are you sure it won't serialize the int if it remains uninitialized?
(I suspect that it still serializes it).

As to your question, I'm not sure. You might try and create a reference type
DateTime class deriving from the System DateTime class. If that reference
type instance is null, perhaps the serializer won't serialize (I haven't
tried this).

-----Original Message-----
From: Moderated discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Clark, Michael
(OFM)
Sent: Wednesday, March 31, 2004 4:51 PM
To: [EMAIL PROTECTED]
Subject: [ADVANCED-DOTNET] Serialization Question

I'm trying to control serialization in a class that contains a couple ints,
some strings, and a date.  At times, each of these elements may be left
uninitialized.  When serialized to xml, the uninitialized ints and strings
are simply not serialized.  For example, using the following class fragment,
if Agency is left blank but the int and the date have values, the class is
serialized without it:
<Class GroupID="1" RefDate="1/1/2004 14:54:33" />

However, if the date is not initialized, it shows up anyway, as in:
<Class Agency="WSOC" GroupID="1" RefDate="0001-01-01T00:00:00.0000000-08:00"
/>

We'd really rather the RefDate attribute not show up at all if the date is
uninitialized, but I cannot find any options to accomplish this.

Anyone know how to do this?

Mike

The class:

[XmlAttribute]
public string Agency
{
        get{return _agency;}
        set{_agency = value;}
}

[XmlAttribute]
public int GroupID
{
        get{return _groupID;}
        set{_groupID = value;}
}

[XmlAttribute]
public DateTime RefDate
{
        get{return _refDate;}
        set{_refDate = value;}
}

===================================
This list is hosted by DevelopMentorR  http://www.develop.com Some .NET
courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

===================================
This list is hosted by DevelopMentor�  http://www.develop.com
Some .NET courses you may be interested in:

NEW! Guerrilla ASP.NET, 17 May 2004, in Los Angeles
http://www.develop.com/courses/gaspdotnetls

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to