Support for ShouldSerialize ?

Apr 23, 2010 at 7:52 AM

Hello,

It seems that Xsd2Code had no support for ShouldSerialize pattern.    http://msdn.microsoft.com/en-us/library/53b8022e(VS.90).aspx     I'm wrong?

ShouldSerialize is very useful when using nullable. A small example:  

    public class Person
    {
        public int ID;
        public String Nome;
        public DateTime BirthDate;
        public int? Counter;

        public bool ShouldSerializeCounter() { return Counter.HasValue; }
    }

If I use ShouldSerialize the serizalized xml is:

<?xml version="1.0" encoding="utf-16"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>1</ID>
<Nome>Fabry</Nome>
<BirthDate>2010-04-23T09:46:14.452458+02:00</BirthDate>
</Person>

If I NOT use ShouldSerialize xml is:

<?xml version="1.0" encoding="utf-16"?><Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ID>1</ID>
<Nome>Fabry</Nome>
<BirthDate>2010-04-23T09:46:26.1564069+02:00</BirthDate>
<Counter xsi:nil="true" />
</Person>

I don't want <Counter> tag if the value is null.

Is there another way to get that using Xsd2Code without support for ShouldSerialize?

 

  thank you

 

   fabrizio