Monday, March 26, 2012

serialization - include collection property

Why the collection property is not included in the ouput
serialization ?

I have a custom generic collection (implements icollection): Events of
objects: Event.

Event is a simple class exposing, let's say, one property: name

Public Class Event
<XmlAttribute("name")_
Public EventName As string
end class

Events Implements ICollection
Public EventColor as string
......

Everything works except when I save the xml file I'm expecting to get
smth like:

<?xml version="1.0" encoding="utf-8"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EVENTCOLOR="some color">
<event eventname="some name" />
</events>

EVENTCOLOR attribute is not in the xml, any idea ?

Thanks,
xkeIn your example EventColor isn't a part of the class Event, and thus
wouldn't get serialized along with it. The only member Event has is
EventName. Is EventName getting serialized? What output are you getting?

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com
"xke" <xkeops@.gmail.comwrote in message
news:1193083502.735588.136230@.e34g2000pro.googlegr oups.com...

Quote:

Originally Posted by

Why the collection property is not included in the ouput
serialization ?
>
I have a custom generic collection (implements icollection): Events of
objects: Event.
>
Event is a simple class exposing, let's say, one property: name
>
Public Class Event
<XmlAttribute("name")_
Public EventName As string
end class
>
>
Events Implements ICollection
Public EventColor as string
......
>
Everything works except when I save the xml file I'm expecting to get
smth like:
>
<?xml version="1.0" encoding="utf-8"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EVENTCOLOR="some color">
<event eventname="some name" />
</events>
>
EVENTCOLOR attribute is not in the xml, any idea ?
>
Thanks,
xke
>


Hi Andrew,

Yes, EventName gets serialized as member of Event class. Question
would be: why the parent collection class properties don't get
serialized ?
Problem I have is not with the Event class but with Events class (the
collection). I thought Events class members will be also serialized.

It's quite basic what I am trying to do, it should work. If you have
any ideas pls help.

Thanks,
xke

On Oct 22, 10:06 pm, "Andrew Faust" <and...@.andrewfaust.comwrote:

Quote:

Originally Posted by

In your example EventColor isn't a part of the class Event, and thus
wouldn't get serialized along with it. The only member Event has is
EventName. Is EventName getting serialized? What output are you getting?
>
--
Andrew Faust
andrew[at]andrewfaust.comhttp://www.andrewfaust.com
>
"xke" <xke...@.gmail.comwrote in message
>
news:1193083502.735588.136230@.e34g2000pro.googlegr oups.com...
>
>
>

Quote:

Originally Posted by

Why the collection property is not included in the ouput
serialization ?


>

Quote:

Originally Posted by

I have a custom generic collection (implements icollection): Events of
objects: Event.


>

Quote:

Originally Posted by

Event is a simple class exposing, let's say, one property: name


>

Quote:

Originally Posted by

Public Class Event
<XmlAttribute("name")_
Public EventName As string
end class


>

Quote:

Originally Posted by

Events Implements ICollection
Public EventColor as string
......


>

Quote:

Originally Posted by

Everything works except when I save the xml file I'm expecting to get
smth like:


>

Quote:

Originally Posted by

<?xml version="1.0" encoding="utf-8"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EVENTCOLOR="some color">
<event eventname="some name" />
</events>


>

Quote:

Originally Posted by

EVENTCOLOR attribute is not in the xml, any idea ?


>

Quote:

Originally Posted by

Thanks,
xke- Hide quoted text -


>
- Show quoted text -


Sorry. Misread your original post.

According to this article the Serializer only supports writing out the
actual objects contained in an ICollection and not the actual properties of
the collection itself.

http://www.diranieh.com/NETSerializ...rialization.htm
I would recommend a slight restructuring to something like this

class Events
{
public string EventColor;
private List<Eventm_Events;
public Event[] UpcomingEvents
{
get { return m_Events.ToArray(); }
set {
m_Events = new List<Event>();
m_Events.AddRange(value);
}
}
}

Then serialize the Events class.

"xke" wrote:

Quote:

Originally Posted by

Hi Andrew,
>
Yes, EventName gets serialized as member of Event class. Question
would be: why the parent collection class properties don't get
serialized ?
Problem I have is not with the Event class but with Events class (the
collection). I thought Events class members will be also serialized.
>
It's quite basic what I am trying to do, it should work. If you have
any ideas pls help.
>
Thanks,
xke
>
On Oct 22, 10:06 pm, "Andrew Faust" <and...@.andrewfaust.comwrote:

Quote:

Originally Posted by

In your example EventColor isn't a part of the class Event, and thus
wouldn't get serialized along with it. The only member Event has is
EventName. Is EventName getting serialized? What output are you getting?

--
Andrew Faust
andrew[at]andrewfaust.comhttp://www.andrewfaust.com

"xke" <xke...@.gmail.comwrote in message

news:1193083502.735588.136230@.e34g2000pro.googlegr oups.com...

Quote:

Originally Posted by

Why the collection property is not included in the ouput
serialization ?


Quote:

Originally Posted by

I have a custom generic collection (implements icollection): Events of
objects: Event.


Quote:

Originally Posted by

Event is a simple class exposing, let's say, one property: name


Quote:

Originally Posted by

Public Class Event
<XmlAttribute("name")_
Public EventName As string
end class


Quote:

Originally Posted by

Events Implements ICollection
Public EventColor as string
......


Quote:

Originally Posted by

Everything works except when I save the xml file I'm expecting to get
smth like:


Quote:

Originally Posted by

<?xml version="1.0" encoding="utf-8"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EVENTCOLOR="some color">
<event eventname="some name" />
</events>


Quote:

Originally Posted by

EVENTCOLOR attribute is not in the xml, any idea ?


Quote:

Originally Posted by

Thanks,
xke- Hide quoted text -


- Show quoted text -


>
>
>


beautiful idea / will give it a try tomorrow

thanks,
xke

On Oct 23, 1:28 pm, Andrew Faust <afa...@.nospam.nospamwrote:

Quote:

Originally Posted by

Sorry. Misread your original post.
>
According to this article the Serializer only supports writing out the
actual objects contained in an ICollection and not the actual properties of
the collection itself.
>
http://www.diranieh.com/NETSerializ...rialization.htm
>
I would recommend a slight restructuring to something like this
>
class Events
{
public string EventColor;
private List<Eventm_Events;
public Event[] UpcomingEvents
{
get { return m_Events.ToArray(); }
set {
m_Events = new List<Event>();
m_Events.AddRange(value);
}
}
>
}
>
Then serialize the Events class.
>
>
>
"xke" wrote:

Quote:

Originally Posted by

Hi Andrew,


>

Quote:

Originally Posted by

Yes, EventName gets serialized as member of Event class. Question
would be: why the parent collection class properties don't get
serialized ?
Problem I have is not with the Event class but with Events class (the
collection). I thought Events class members will be also serialized.


>

Quote:

Originally Posted by

It's quite basic what I am trying to do, it should work. If you have
any ideas pls help.


>

Quote:

Originally Posted by

Thanks,
xke


>

Quote:

Originally Posted by

On Oct 22, 10:06 pm, "Andrew Faust" <and...@.andrewfaust.comwrote:

Quote:

Originally Posted by

In your example EventColor isn't a part of the class Event, and thus
wouldn't get serialized along with it. The only member Event has is
EventName. Is EventName getting serialized? What output are you getting?


>

Quote:

Originally Posted by

Quote:

Originally Posted by

--
Andrew Faust
andrew[at]andrewfaust.comhttp://www.andrewfaust.com


>

Quote:

Originally Posted by

Quote:

Originally Posted by

"xke" <xke...@.gmail.comwrote in message


>

Quote:

Originally Posted by

Quote:

Originally Posted by

>news:1193083502.735588.136230@.e34g2000pro.googlegr oups.com...


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Why the collection property is not included in the ouput
serialization ?


>

Quote:

Originally Posted by

Quote:

Originally Posted by

I have a custom generic collection (implements icollection): Events of
objects: Event.


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Event is a simple class exposing, let's say, one property: name


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Public Class Event
<XmlAttribute("name")_
Public EventName As string
end class


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Events Implements ICollection
Public EventColor as string
......


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Everything works except when I save the xml file I'm expecting to get
smth like:


>

Quote:

Originally Posted by

Quote:

Originally Posted by

<?xml version="1.0" encoding="utf-8"?>
<events xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" EVENTCOLOR="some color">
<event eventname="some name" />
</events>


>

Quote:

Originally Posted by

Quote:

Originally Posted by

EVENTCOLOR attribute is not in the xml, any idea ?


>

Quote:

Originally Posted by

Quote:

Originally Posted by

Thanks,
xke- Hide quoted text -


>

Quote:

Originally Posted by

Quote:

Originally Posted by

- Show quoted text -- Hide quoted text -


>
- Show quoted text -

0 comments:

Post a Comment