473,396 Members | 1,832 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,396 software developers and data experts.

Serializing array of userdefined types

Hello,

I want to have a class that contains only a collection of another class.
For example:

public __gc class Alignment {
public:
Alignment();
...
};

// Probably using XmlIncludeAttribute such as this is wrong...
[System::Xml::Serialization::XmlIncludeAttribute(__ typeof(Alignment))]
public __gc class Alignments : public System::Collections::ArrayList {
public:
Alignments();
...
};

I figured I could use Alignments like this:
Alignment* alignment = new Alignment;
Alignments* alignments = new Alignments;
alignments->Add(alignment);

BTW, I am using XmlSerializer and XmlTextWriter.

But I get exceptions:
--------- Exception Data ---------
Message: There was an error reflecting type 'LX.LandXML'.
Exception Type: System.InvalidOperationException
Source: System.Xml
StrackTrace: at
System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, Boolean repeats)
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortElement(TypeModel
model, XmlRootAttribute root, String defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type, String
defaultNamespace)
at System.Xml.Serialization.XmlSerializer..ctor(Type type)
at main(Int32 argc, SByte** argv) in c:\documents and
settings\daniel.sbg\my documents\visual studio
projects\managed_test\test1\test1.cpp:line 22
TargetSite: System.Xml.Serialization.TypeMapping
ImportTypeMapping(System.Xml.Serialization.TypeMod el, System.String,
ImportContext, System.String, Boolean)
--------- Exception Data ---------
Message: There was an error reflecting property 'Alignments'.
Exception Type: System.InvalidOperationException
Source: System.Xml
StrackTrace: at
System.Xml.Serialization.XmlReflectionImporter.Imp ortStructLikeMapping(StructModel
model, String ns)
at
System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, Boolean repeats)
TargetSite: System.Xml.Serialization.StructMapping
ImportStructLikeMapping(System.Xml.Serialization.S tructModel,
System.String)
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>

Any pointers or references very much appreciated.
Thanks!

--
Daniel
Nov 12 '05 #1
3 2526
Hello, Daniel!
You wrote on Wed, 19 May 2004 12:09:36 +0200:
[Sorry, skipped]
DL> What is the usual way to serialize a collection of the same type of
DL> objects? The end result I am after is this:
DL> <Alignments>
DL> <Alignment name="Centreline>
DL> </Alignment>
DL> <Alignment name="LeftCatch">
DL> </Alignment>
DL> ... arbitrary number of Alignments
DL> </Alignments>

DL> Any pointers or references very much appreciated.
DL> Thanks!

Well, maybe this example be right for you.
Expand|Select|Wrap|Line Numbers
  1. #using <mscorlib.dll>
  2. #using <System.Xml.dll>
  3.  
  4. using namespace System;
  5. using namespace System::Collections;
  6. using namespace System::Xml;
  7. using namespace System::Xml::Serialization;
  8. using namespace System::IO;
  9.  
  10. public __gc class foo
  11. {
  12. public:
  13. foo()
  14. {
  15. }
  16.  
  17. foo(System::String* s)
  18. {
  19. _s = s;
  20. }
  21. public:
  22. [XmlAttributeAttribute(S"name")]
  23. System::String* _s;
  24. };
  25.  
  26. public __gc class foos
  27. {
  28. public:
  29. foos()
  30. {
  31. arr = new ArrayList();
  32. }
  33. public:
  34. [XmlArray(S"Alignments")]
  35. [XmlArrayItemAttribute(S"Alignment",__typeof(foo))]
  36. ArrayList* arr;
  37. };
  38.  
  39. int _tmain()
  40. {
  41. XmlSerializer* xs = new XmlSerializer(__typeof(foo));
  42. foo* f = new foo(S"hi!");
  43. Text::StringBuilder* sb = new Text::StringBuilder();
  44. StringWriter* sw = new StringWriter(sb);
  45. xs->Serialize(sw,f);
  46. sb->Append(S"\n-------\n");
  47. foos* fs = new foos();
  48. fs->arr->Add(f);
  49. xs = new XmlSerializer(__typeof(foos));
  50. xs->Serialize(sw,fs);
  51. Console::WriteLine(sb->ToString());
  52. return 0;
  53. }
  54.  
With best regards, Alex Shirshov.
Nov 12 '05 #2
On Thu, 20 May 2004 12:39:27 +0400, Alex Shirshov wrote:
Hello, Daniel!
You wrote on Wed, 19 May 2004 12:09:36 +0200:
[...]
With best regards, Alex Shirshov.


Thanks a lot Alex! This is just what I needed.

--
Daniel
Nov 12 '05 #3
On Wed, 19 May 2004 12:09:36 +0200, Daniel Lidström wrote:
Hello,
[...]
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!

--
Daniel
Nov 12 '05 #4

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

Similar topics

1
by: Ryan Hubbard | last post by:
I've have been trying to serialize an array of objects. I can serialize and set a session variable and retrieve it successfully for a serialize string of any length within the contents of a single...
2
by: Werner B. Strydom | last post by:
Hi I wrote a class, Document, which has an Array of ITask objects. In order to serialize the tasks contained within the tasks array, I marked each task class with an attribute , and added an...
0
by: Matt Parker | last post by:
Hi I was wondering if anyone would be able to help me with this problem. I have an annotated schema that I use to query my database and fill a dataset using the sqlxmladapter. The call to...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
10
by: copx | last post by:
I want to save a struct to disk.... as plain text. At the moment I do it with a function that just writes the data using fprintf. I mean like this: fprintf(fp, "%d %d", my_struct.a, my_struct.b)...
2
by: Earl Teigrob | last post by:
I am saving and restoring value types such as Int32, DateTime and Boolean in strings. I was wondering if there is a mechanism build into .NET for serializing and deserializing these to string...
2
by: Rick Francis | last post by:
I need help serializing an array without including the array "name". I am writing in C# and using the XmlSerializer to serial classes. I am trying to serialize a class with an array in it like...
2
by: geshas | last post by:
Hello, I need to serialize/deserialize array of non-typed objects to/from such xml: <contents> <resource name="res1" /> <category name="cat1" /> <resource name="res2" /> <category...
6
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.