xml serialization

  • Thread starter rjn rjn via .NET 247
  • Start date
R

rjn rjn via .NET 247

Hi

I'm using XML Serialization for serializing an object. I read aset of records from the database and populate the members of theobject if the value is not null. eg., I have an Employee objectwhich has its members Id,name and address. If the Id is not nullthen I will populate its value from that I got from the databse.When I serialize the object , I find that xml structure willalways have the Id column either with the actual value or withvalue 0 if the value is Null in the databse.
<employees>
<employee><Id>111</Id><name>aaa</name></employee>
<employee><Id>0</Id></name></employee>
</employees>

My feeling is this is the default behaviour of xml serializer fordata types int, boolean and date and will have a value0,0001-01-01 and false as default values respectively.

I cannot set the property <XmlElement(IsNullable:=True)> forvalue types. Is there any way where I can avoid serializingtypes of int,date and boolean when they have null values in thedatabase.

Regards

RJN
 
N

Nicholas Paldino [.NET/C# MVP]

I think the best bet in this situation would be to use the SqlInt32
class (or the SqlBoolean, etc, etc). You will always have an instance of
it, but it will have the IsNull property and the Value property which will
get serialized. The IsNull property is what you need to determine null.

Of course, this means that you have to change the property definition to
use these types, but it would work. If you don't want to do that, then you
will have to store boolean values internally which will store whether or not
the value is null.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Hi

I'm using XML Serialization for serializing an object. I read a set of
records from the database and populate the members of the object if the
value is not null. eg., I have an Employee object which has its members
Id,name and address. If the Id is not null then I will populate its value
from that I got from the databse. When I serialize the object , I find that
xml structure will always have the Id column either with the actual value or
with value 0 if the value is Null in the databse.
<employees>
<employee><Id>111</Id><name>aaa</name></employee>
<employee><Id>0</Id></name></employee>
</employees>

My feeling is this is the default behaviour of xml serializer for data types
int, boolean and date and will have a value 0,0001-01-01 and false as
default values respectively.

I cannot set the property <XmlElement(IsNullable:=True)> for value types. Is
there any way where I can avoid serializing types of int,date and boolean
when they have null values in the database.

Regards

RJN
 
M

Mickey Williams

I cannot set the property <XmlElement(IsNullable:=True)> for value types. Is
there any way where I can avoid serializing types of int,date and boolean
when they have null values in the database.


If you just want to supress the generation of the XML in those cases, you
need to build some serialization hints into your type. Given a property
named Age, a boolean property named AgeSpecified will control whether or not
the serializer emits XML for the Age property. Add an XmlIgnore attribute to
prevent the flag from being serialized, as shown here:

public class Foo
{
int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}

bool _ageSpecified = false;
[XmlIgnore]
public bool AgeSpecified
{
get { return _ageSpecified; }
set { _ageSpecified = value; }
}
}

Serializing the two instance of Foo below will generate different
documents -- the second instance will not create any Age element.

Foo f1 = new Foo();
f1.Age = 42;
f1.AgeSpecified = true;

Foo f2 = new Foo();
f2.AgeSpecified = false;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top