XML Serialization producing empty element when class has attributes
-
I'm trying to serialise a graph structure to XML. Everything is working fine except for one object which is alwasy coming out as an empty element. Even if I serialise it on its own. Its the most important object as it represents the link. Any idea why it wouldn't show any attributes? Heres the code:
[Serializable()] [System.Xml.Serialization.XmlInclude(typeof(ParticipantNode))] public class EdgeToNeighbour { #region Private Member Variables private string id; private Node neighbour; #endregion #region Constructors public EdgeToNeighbour() { } public EdgeToNeighbour(Node neighbour) { this.neighbour = neighbour; this.id = neighbour.Key; } #endregion #region Properties /// /// The neighbour the edge is linking to. /// public Node Neighbour { get { return neighbour; } } public string ID { get{return id;} } #endregion }
I've tried ignoring the complex type, i've tried adding XML text, I've tried lots of different combinations of XmlAttribute etc. but i always end up with this: Serialised on its own:<EdgeToNeighbour xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
Serialised as part of the graph structure:<anyType xsi:type="Node"> <Data> <ID>2</ID> <TypeID>0</TypeID> <Name>A Node</Name> </Data> <Edges> <EdgeToNeighbour /> <EdgeToNeighbour /> </Edges> </anyType>
All I want is for edge to neighbour object to show the attribute ID so I can see the nodes it is linked to. Thanks in advance. Rob. -
I'm trying to serialise a graph structure to XML. Everything is working fine except for one object which is alwasy coming out as an empty element. Even if I serialise it on its own. Its the most important object as it represents the link. Any idea why it wouldn't show any attributes? Heres the code:
[Serializable()] [System.Xml.Serialization.XmlInclude(typeof(ParticipantNode))] public class EdgeToNeighbour { #region Private Member Variables private string id; private Node neighbour; #endregion #region Constructors public EdgeToNeighbour() { } public EdgeToNeighbour(Node neighbour) { this.neighbour = neighbour; this.id = neighbour.Key; } #endregion #region Properties /// /// The neighbour the edge is linking to. /// public Node Neighbour { get { return neighbour; } } public string ID { get{return id;} } #endregion }
I've tried ignoring the complex type, i've tried adding XML text, I've tried lots of different combinations of XmlAttribute etc. but i always end up with this: Serialised on its own:<EdgeToNeighbour xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
Serialised as part of the graph structure:<anyType xsi:type="Node"> <Data> <ID>2</ID> <TypeID>0</TypeID> <Name>A Node</Name> </Data> <Edges> <EdgeToNeighbour /> <EdgeToNeighbour /> </Edges> </anyType>
All I want is for edge to neighbour object to show the attribute ID so I can see the nodes it is linked to. Thanks in advance. Rob.All of you properties are either private or read only. Only public read/write properties are serialized unless you implement the ISerializable interface(which lets you determine exactly how your class is serialized). -- modified at 20:58 Wednesday 23rd May, 2007
topcoderjax - Remember, Google is your friend.