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

Problem with collection classes

Hi,

how do I synchronize (to XML using XmlSerializer) something to this:
<Alignments name="Road Project">
<Alignment name="Centerline">
<CoordGeom>
<Line staStart="0">
<Start>2000 6000</Start>
<End>2186.841 6068.005</End>
</Line>
<Curve rot="cw" />
... arbitrary number of Line's and Curve's
... in arbitrary order
</CoordGeom>
... arbitrary number of CoordGeom's ...
</Alignment>
.... arbitrary number of Alignment's ...
</Alignments>

What troubles me is how to deal with the array of CoordGeom's, which are
generic arrays in the sense that they can contain either a Line or a Curve,
and any number of them.

I am using CS and have an array for each type as of now, i.e. one array for
Curve's and one for Line's. But this does not allow me to mix the order in
which they occur, so the Curve and Line elements have to be in the same
ArrayList.

I want to know how to structure my classes and what to make XmlArray of.
Any tips you can give me is much appreciated, thank you.

--
Daniel
Nov 12 '05 #1
6 2098
No answer...

Let me describe what I am doing. I've created these classes:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Motorcycle))]
public class Vehicle
{
public Vehicle() {}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
}
public class ParkingLot
{
public ParkingLot()
{
ParkedVehicles = new ArrayList();
ParkedVehicles.Add(new Car());
ParkedVehicles.Add(new Motorcycle());
}
[XmlElement(ElementName="ParkedCar",Type=typeof(Car ))]
[XmlElement(ElementName="ParkedMotorcycle",Type=typ eof(Motorcycle))]
public ArrayList ParkedVehicles;
}

Serializing a ParkingLot creates this (which is just what I want):
<?xml version="1.0" encoding="utf-8"?>
<ParkingLot xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>

As you can see there is no parent element around parked car and parked
motorcycle, i.e. no ParkedVehicles element.

Now, I would like to have a collection of ParkingLot's inside another
class. So I add this:
public ArrayList ParkingLotCollection;
to my other class. Trying to serialize produces an exception:
Message: The type LX.ParkingLot was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.

So I rewrite it like this:

[XmlArrayItem(Type=typeof(ParkingLot))]
public ArrayList ParkingLotCollection;

But this creates a parent element around my ParkingLot's, like this:
<ParkingLotCollection>
<ParkingLot>
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>
</ParkingLotCollection>

From what I've read I should be able to remove this parent element by
adding a empty XmlElement attribute to my ArrayList ParkingLotCollection:

[XmlArrayItem(Type=typeof(ParkingLot))]
[XmlElement]
public ArrayList ParkingLotCollection;

But this causes an exception:
Message: There was an error reflecting field 'ParkingLotCollection'.
I have no idea what to do. What am I doing wrong? Any information is very
much appreciated. Thanks.

--
Daniel
Nov 12 '05 #2
No answer...

Let me describe what I am doing. I've created these classes:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Motorcycle))]
public class Vehicle
{
public Vehicle() {}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
}
public class ParkingLot
{
public ParkingLot()
{
ParkedVehicles = new ArrayList();
ParkedVehicles.Add(new Car());
ParkedVehicles.Add(new Motorcycle());
}
[XmlElement(ElementName="ParkedCar",Type=typeof(Car ))]
[XmlElement(ElementName="ParkedMotorcycle",Type=typ eof(Motorcycle))]
public ArrayList ParkedVehicles;
}

Serializing a ParkingLot creates this (which is just what I want):
<?xml version="1.0" encoding="utf-8"?>
<ParkingLot xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>

As you can see there is no parent element around parked car and parked
motorcycle, i.e. no ParkedVehicles element.

Now, I would like to have a collection of ParkingLot's inside another
class. So I add this:
public ArrayList ParkingLotCollection;
to my other class. Trying to serialize produces an exception:
Message: The type LX.ParkingLot was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.

So I rewrite it like this:

[XmlArrayItem(Type=typeof(ParkingLot))]
public ArrayList ParkingLotCollection;

But this creates a parent element around my ParkingLot's, like this:
<ParkingLotCollection>
<ParkingLot>
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>
</ParkingLotCollection>

From what I've read I should be able to remove this parent element by
adding a empty XmlElement attribute to my ArrayList ParkingLotCollection:

[XmlArrayItem(Type=typeof(ParkingLot))]
[XmlElement]
public ArrayList ParkingLotCollection;

But this causes an exception:
Message: There was an error reflecting field 'ParkingLotCollection'.
I have no idea what to do. What am I doing wrong? Any information is very
much appreciated. Thanks.

--
Daniel
Nov 12 '05 #3
I don't understand.
If a class contains a ParkingLotCollection (a Mall?) , then shouldn't each
Lot in the collection be serialized independently?

<Mall>
<Lot>
<ParkedCar>
<Make>Ijbnxnwhna</Make>
<Year>1995</Year>
<Color>red</Color>
<VIN>582949646792293965529740</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Ggikttsgpp</Make>
<Year>1985</Year>
<Color>red</Color>
</ParkedMotorcycle>
</Lot>
<Lot>
<ParkedMotorcycle>
<Make>Dpdiqdccez</Make>
<Year>1986</Year>
<Color>green</Color>
</ParkedMotorcycle>
<ParkedCar>
<Make>Gbulediyje</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>331456735041360460902400</VIN>
</ParkedCar>
</Lot>
</Mall>
What would you like to see happen?
By the way, the extra layer of structure isn't contributing anything to the
issue.
Suppose you had a Lot like this:

<ParkingLot>
<ParkedCar>
<Make>Dgvpadixcx</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>045509407786350954614071</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Htvlwawaua</Make>
<Year>1985</Year>
<Color>blue</Color>
</ParkedMotorcycle>
</ParkingLot>
What you seem to want is to serialize the collection (in the Lot's case, it
is a collection of vehicles) without specifically identifying each vehicle.
Maybe like so:
<ParkingLot>
<Make>Dgvpadixcx</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>045509407786350954614071</VIN>
<Make>Htvlwawaua</Make>
<Year>1985</Year>
<Color>blue</Color>
</ParkingLot>
What does that get you?

-D

"Daniel Lidström" <so*****@microsoft.com> wrote in message
news:1h****************************@40tude.net...
No answer...

Let me describe what I am doing. I've created these classes:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Motorcycle))]
public class Vehicle
{
public Vehicle() {}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
}
public class ParkingLot
{
public ParkingLot()
{
ParkedVehicles = new ArrayList();
ParkedVehicles.Add(new Car());
ParkedVehicles.Add(new Motorcycle());
}
[XmlElement(ElementName="ParkedCar",Type=typeof(Car ))]
[XmlElement(ElementName="ParkedMotorcycle",Type=typ eof(Motorcycle))]
public ArrayList ParkedVehicles;
}

Serializing a ParkingLot creates this (which is just what I want):
<?xml version="1.0" encoding="utf-8"?>
<ParkingLot xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>

As you can see there is no parent element around parked car and parked
motorcycle, i.e. no ParkedVehicles element.

Now, I would like to have a collection of ParkingLot's inside another
class. So I add this:
public ArrayList ParkingLotCollection;
to my other class. Trying to serialize produces an exception:
Message: The type LX.ParkingLot was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.

So I rewrite it like this:

[XmlArrayItem(Type=typeof(ParkingLot))]
public ArrayList ParkingLotCollection;

But this creates a parent element around my ParkingLot's, like this:
<ParkingLotCollection>
<ParkingLot>
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>
</ParkingLotCollection>

From what I've read I should be able to remove this parent element by
adding a empty XmlElement attribute to my ArrayList ParkingLotCollection:

[XmlArrayItem(Type=typeof(ParkingLot))]
[XmlElement]
public ArrayList ParkingLotCollection;

But this causes an exception:
Message: There was an error reflecting field 'ParkingLotCollection'.
I have no idea what to do. What am I doing wrong? Any information is very
much appreciated. Thanks.

--
Daniel

Nov 12 '05 #4
I don't understand.
If a class contains a ParkingLotCollection (a Mall?) , then shouldn't each
Lot in the collection be serialized independently?

<Mall>
<Lot>
<ParkedCar>
<Make>Ijbnxnwhna</Make>
<Year>1995</Year>
<Color>red</Color>
<VIN>582949646792293965529740</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Ggikttsgpp</Make>
<Year>1985</Year>
<Color>red</Color>
</ParkedMotorcycle>
</Lot>
<Lot>
<ParkedMotorcycle>
<Make>Dpdiqdccez</Make>
<Year>1986</Year>
<Color>green</Color>
</ParkedMotorcycle>
<ParkedCar>
<Make>Gbulediyje</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>331456735041360460902400</VIN>
</ParkedCar>
</Lot>
</Mall>
What would you like to see happen?
By the way, the extra layer of structure isn't contributing anything to the
issue.
Suppose you had a Lot like this:

<ParkingLot>
<ParkedCar>
<Make>Dgvpadixcx</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>045509407786350954614071</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Htvlwawaua</Make>
<Year>1985</Year>
<Color>blue</Color>
</ParkedMotorcycle>
</ParkingLot>
What you seem to want is to serialize the collection (in the Lot's case, it
is a collection of vehicles) without specifically identifying each vehicle.
Maybe like so:
<ParkingLot>
<Make>Dgvpadixcx</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>045509407786350954614071</VIN>
<Make>Htvlwawaua</Make>
<Year>1985</Year>
<Color>blue</Color>
</ParkingLot>
What does that get you?

-D

"Daniel Lidström" <so*****@microsoft.com> wrote in message
news:1h****************************@40tude.net...
No answer...

Let me describe what I am doing. I've created these classes:
[XmlInclude(typeof(Car))]
[XmlInclude(typeof(Motorcycle))]
public class Vehicle
{
public Vehicle() {}
public string Make;
public string Model;
public int Year;
}
public class Car : Vehicle
{
public Car() {}
public string VIN;
}
public class Motorcycle : Vehicle
{
public Motorcycle() {}
}
public class ParkingLot
{
public ParkingLot()
{
ParkedVehicles = new ArrayList();
ParkedVehicles.Add(new Car());
ParkedVehicles.Add(new Motorcycle());
}
[XmlElement(ElementName="ParkedCar",Type=typeof(Car ))]
[XmlElement(ElementName="ParkedMotorcycle",Type=typ eof(Motorcycle))]
public ArrayList ParkedVehicles;
}

Serializing a ParkingLot creates this (which is just what I want):
<?xml version="1.0" encoding="utf-8"?>
<ParkingLot xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>

As you can see there is no parent element around parked car and parked
motorcycle, i.e. no ParkedVehicles element.

Now, I would like to have a collection of ParkingLot's inside another
class. So I add this:
public ArrayList ParkingLotCollection;
to my other class. Trying to serialize produces an exception:
Message: The type LX.ParkingLot was not expected. Use the XmlInclude or
SoapInclude attribute to specify types that are not known statically.

So I rewrite it like this:

[XmlArrayItem(Type=typeof(ParkingLot))]
public ArrayList ParkingLotCollection;

But this creates a parent element around my ParkingLot's, like this:
<ParkingLotCollection>
<ParkingLot>
<ParkedCar>
<Year>0</Year>
</ParkedCar>
<ParkedMotorcycle>
<Year>0</Year>
</ParkedMotorcycle>
</ParkingLot>
</ParkingLotCollection>

From what I've read I should be able to remove this parent element by
adding a empty XmlElement attribute to my ArrayList ParkingLotCollection:

[XmlArrayItem(Type=typeof(ParkingLot))]
[XmlElement]
public ArrayList ParkingLotCollection;

But this causes an exception:
Message: There was an error reflecting field 'ParkingLotCollection'.
I have no idea what to do. What am I doing wrong? Any information is very
much appreciated. Thanks.

--
Daniel

Nov 12 '05 #5
On Fri, 25 Jun 2004 14:12:25 -0400, Dino Chiesa [Microsoft] wrote:
I don't understand.
If a class contains a ParkingLotCollection (a Mall?) , then shouldn't each
Lot in the collection be serialized independently?

<Mall>
<Lot>
<ParkedCar>
<Make>Ijbnxnwhna</Make>
<Year>1995</Year>
<Color>red</Color>
<VIN>582949646792293965529740</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Ggikttsgpp</Make>
<Year>1985</Year>
<Color>red</Color>
</ParkedMotorcycle>
</Lot>
<Lot>
<ParkedMotorcycle>
<Make>Dpdiqdccez</Make>
<Year>1986</Year>
<Color>green</Color>
</ParkedMotorcycle>
<ParkedCar>
<Make>Gbulediyje</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>331456735041360460902400</VIN>
</ParkedCar>
</Lot>
</Mall>
What would you like to see happen?


That is what I would like to have. Only that I would like to be able to
have more than one Mall.

Maybe I should have told you right away what I really want. I want to
serialize the following (well, a subset actually):
http://landxml.org/schema/landxml-1....LandXMLDoc.htm

I'm having a lot of trouble with the Alignment and Alignments elements in
the LandXML element. There is a sample file here:

http://www.landxml.org/schema/LandXM...20Profiles.xml

If you have a look at it, you will see that it is possible to have several
Alignments' within the LandXML element, and within each Alignments it
should be possible to have several Alignment's, although this sample only
has one. This sample shows several Alignment's within each Alignments:
http://www.landxml.org/schema/LandXM...k/hidden_m.xml

The LandXML specification says something like this:
LandXML can contain 1-oo (oo == infinite) number of Alignments. Alignments
can contain 1-oo number of Alignment's. Alignment's are collections of
CoordGeoms, which can contain Line's, Curve's, or Spiral's (in a specific
order!).

Dino, do you know how I can serialize this? You can use the parkinglot
example if you'd like. I hope I have understood LandXML the right way. If
so, it should be equivalent to the parkinglot at the top of this message,
with the possibility to have 1-oo Mall's.

Thank you!

--
Daniel
Nov 12 '05 #6
On Fri, 25 Jun 2004 14:12:25 -0400, Dino Chiesa [Microsoft] wrote:
I don't understand.
If a class contains a ParkingLotCollection (a Mall?) , then shouldn't each
Lot in the collection be serialized independently?

<Mall>
<Lot>
<ParkedCar>
<Make>Ijbnxnwhna</Make>
<Year>1995</Year>
<Color>red</Color>
<VIN>582949646792293965529740</VIN>
</ParkedCar>
<ParkedMotorcycle>
<Make>Ggikttsgpp</Make>
<Year>1985</Year>
<Color>red</Color>
</ParkedMotorcycle>
</Lot>
<Lot>
<ParkedMotorcycle>
<Make>Dpdiqdccez</Make>
<Year>1986</Year>
<Color>green</Color>
</ParkedMotorcycle>
<ParkedCar>
<Make>Gbulediyje</Make>
<Year>1998</Year>
<Color>blue</Color>
<VIN>331456735041360460902400</VIN>
</ParkedCar>
</Lot>
</Mall>
What would you like to see happen?


That is what I would like to have. Only that I would like to be able to
have more than one Mall.

Maybe I should have told you right away what I really want. I want to
serialize the following (well, a subset actually):
http://landxml.org/schema/landxml-1....LandXMLDoc.htm

I'm having a lot of trouble with the Alignment and Alignments elements in
the LandXML element. There is a sample file here:

http://www.landxml.org/schema/LandXM...20Profiles.xml

If you have a look at it, you will see that it is possible to have several
Alignments' within the LandXML element, and within each Alignments it
should be possible to have several Alignment's, although this sample only
has one. This sample shows several Alignment's within each Alignments:
http://www.landxml.org/schema/LandXM...k/hidden_m.xml

The LandXML specification says something like this:
LandXML can contain 1-oo (oo == infinite) number of Alignments. Alignments
can contain 1-oo number of Alignment's. Alignment's are collections of
CoordGeoms, which can contain Line's, Curve's, or Spiral's (in a specific
order!).

Dino, do you know how I can serialize this? You can use the parkinglot
example if you'd like. I hope I have understood LandXML the right way. If
so, it should be equivalent to the parkinglot at the top of this message,
with the possibility to have 1-oo Mall's.

Thank you!

--
Daniel
Nov 12 '05 #7

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

Similar topics

1
by: CodeMonkey | last post by:
Hi, I am trying to use xml serialization to simplify load/save functions in some classes i have created and am hitting a few problems. Any help to either would be most appreciated. I have...
1
by: Terry | last post by:
I created a collection derived from CollectionBase that is just made up of strings, called "StringList". I have another object that has as a member one of these StringList. If I assign that...
2
by: Edward Diener | last post by:
In C++ an overridden virtual function in a derived class must have the exact same signature of the function which is overridden in the base class, except for the return type which may return a...
7
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the...
11
by: EDom | last post by:
Hi, I have aspnet_wp.exe with increasing on every postback and not every revisit to any page. Even if I clear session and close the browser it remains in the memory. I have only one connection...
7
by: Joe | last post by:
I've tracked the performance issue down to a single class. This class derives from CollectionBase and stores a basic value type such as string, int, double, etc... I also store the type itself...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
3
by: Kaba | last post by:
Hello I have a design problem, which I don't seem to have a good solution. By example: class B; class C; class D {
7
by: =?Utf-8?B?Q29kZVJhem9y?= | last post by:
Can someone explain a few things about collections to me. In C# 2 generics, you can create a collection class by inheriting from System.Collections.ObjectModel.Collection. Using this you can...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.