473,382 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

xml serialization

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

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>kXtPhKBi506Nm6J+4QneYg==</Id>
Nov 16 '05 #1
2 2860
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]
- mv*@spam.guard.caspershouse.com
"rjn rjn via .NET 247" <an*******@dotnet247.com> wrote in message
news:OD******************@tk2msftngp13.phx.gbl...
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

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>kXtPhKBi506Nm6J+4QneYg==</Id>
Nov 16 '05 #2
"rjn rjn via .NET 247" <an*******@dotnet247.com> wrote in message
news:OD******************@tk2msftngp13.phx.gbl...
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;

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined...
1
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have...
3
by: Aaron Clamage | last post by:
Hi, I'm not sure that if this is the right forum, but any help would be greatly appreciated. I am porting some java serialization code to c# and I can't figure out the correct way to do it. ...
6
by: Uttam | last post by:
Hello, We are at a very crucial decision making stage to select between .Net and Java. Our requirement is to download a class at runtime on the client computer and execute it using remoting or...
3
by: Alexander | last post by:
When i store rule on PC with .NET.SP1 i cant restore them from PC without SP1. An i get this Error: System.Runtime.Serialization.SerializationException: Possible Version mismatch. Type...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
5
by: Nikola Skoric | last post by:
I ran in Mono a program developed on .NET Framework 2.0 and it ran OK until I tried to desirialize a object. There the program died abruptly dumping this: System.ArgumentOutOfRangeException:...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
1
by: kikisan | last post by:
I am developing a windows service which utilizes the following classes: interface IPersistable; abstract class PersistableObject : IPersistable;
2
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.