[quoted text, click to view] On Wed, 19 May 2004 12:09:36 +0200, Daniel Lidström wrote:
> Hello,
[...]
[quoted text, click to view] > What is the usual way to serialize a collection of the same type of
> objects? The end result I am after is this:
> <Alignments>
> <Alignment name="Centreline>
> </Alignment>
> <Alignment name="LeftCatch">
> </Alignment>
> ... arbitrary number of Alignments
> </Alignments>
New problem: The end result I am after is this:
<Alignments name="Road Project">
<Alignment name="Centerline" />
<Alignment name="Centerline 2" />
... arbitrary number of these
</Alignments>
What I get right now is this:
<Alignments name="Road Project">
<Alignment>
<Alignment name="Centerline" />
<Alignment name="Centerline 2" />
</Alignment>
</Alignments>
I am doing it this way (everything is in namespace LX):
public __gc class LandXML {
public:
LandXML() { }
__property LX::Alignments* get_Alignments() { return m_arr; }
__property void set_Alignments(LX::Alignments* a) { m_arr = a; }
protected:
LX::Alignments* m_arr;
};
public __gc class Alignment {
public:
Alignment() { }
[XmlAttributeAttribute("name")]
__property String* get_name() { return m_name; }
__property void set_name(String* n) { m_name = n; }
protected:
String* m_name;
};
public __gc class Alignments {
public:
Alignments() { m_array = new ArrayList(); }
[XmlAttributeAttribute("name")]
__property System::String* get_name() { return m_name; }
__property void set_name(System::String* n) { m_name = n; }
[XmlArrayItemAttribute(Type=__typeof(LX::Alignment))]
__property ArrayList* get_Alignment() { return m_array; }
__property void set_Alignment(ArrayList* arr) { m_array = arr; }
void Add(LX::Alignment* alignment) { m_array->Add(alignment); }
protected:
String* m_name;
ArrayList* m_array;
};
Somewhere in main.cpp I set the elements of a LandXml object, and serialize
object to disk:
XmlSerializer* ser = new XmlSerializer(__typeof(LandXML));
XmlTextWriter* writer = new XmlTextWriter("out.xml", Encoding::UTF8);
// write a human readable file
writer->Formatting = Formatting::Indented;
LandXML* land_xml = new LandXML();
Alignment* alignment = new Alignment;
Alignment* alignment2 = new Alignment;
alignment->name = "Centerline";
alignment2->name = "Centerline 2";
land_xml->Alignments->Add(alignment);
land_xml->Alignments->Add(alignment2);
land_xml->Alignments->name = "Road Project";
ser->Serialize(writer, land_xml);
writer->Close();
So it seems I need to remove one level of Alignment, only I don't really
know how to do it. If I create a class Alignment that instead of having a
ArrayList as member, I let it inherit from ArrayList. Then it doesn't
matter if I have a attribute called name, because it will not be
serialized. How can I solve this problem? Any help in form of samples,
links, docs, very much appreciated.
Thanks!
--