Problem with collection classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Lidström

    Problem with collection classes

    Hi,

    how do I synchronize (to XML using XmlSerializer) something to this:
    <Alignments name="Road Project">
    <Alignment name="Centerlin e">
    <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
  • Daniel Lidström

    #2
    Re: Problem with collection classes

    No answer...

    Let me describe what I am doing. I've created these classes:
    [XmlInclude(type of(Car))]
    [XmlInclude(type of(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(Elem entName="Parked Car",Type=typeo f(Car))]
    [XmlElement(Elem entName="Parked Motorcycle",Typ e=typeof(Motorc ycle))]
    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>
    <ParkedMotorcyc le>
    <Year>0</Year>
    </ParkedMotorcycl e>
    </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 ParkingLotColle ction;
    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(Ty pe=typeof(Parki ngLot))]
    public ArrayList ParkingLotColle ction;

    But this creates a parent element around my ParkingLot's, like this:
    <ParkingLotColl ection>
    <ParkingLot>
    <ParkedCar>
    <Year>0</Year>
    </ParkedCar>
    <ParkedMotorcyc le>
    <Year>0</Year>
    </ParkedMotorcycl e>
    </ParkingLot>
    </ParkingLotColle ction>

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

    [XmlArrayItem(Ty pe=typeof(Parki ngLot))]
    [XmlElement]
    public ArrayList ParkingLotColle ction;

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

    --
    Daniel

    Comment

    • Daniel Lidström

      #3
      Re: Problem with collection classes

      No answer...

      Let me describe what I am doing. I've created these classes:
      [XmlInclude(type of(Car))]
      [XmlInclude(type of(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(Elem entName="Parked Car",Type=typeo f(Car))]
      [XmlElement(Elem entName="Parked Motorcycle",Typ e=typeof(Motorc ycle))]
      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>
      <ParkedMotorcyc le>
      <Year>0</Year>
      </ParkedMotorcycl e>
      </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 ParkingLotColle ction;
      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(Ty pe=typeof(Parki ngLot))]
      public ArrayList ParkingLotColle ction;

      But this creates a parent element around my ParkingLot's, like this:
      <ParkingLotColl ection>
      <ParkingLot>
      <ParkedCar>
      <Year>0</Year>
      </ParkedCar>
      <ParkedMotorcyc le>
      <Year>0</Year>
      </ParkedMotorcycl e>
      </ParkingLot>
      </ParkingLotColle ction>

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

      [XmlArrayItem(Ty pe=typeof(Parki ngLot))]
      [XmlElement]
      public ArrayList ParkingLotColle ction;

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

      --
      Daniel

      Comment

      • Dino Chiesa [Microsoft]

        #4
        Re: Problem with collection classes

        I don't understand.
        If a class contains a ParkingLotColle ction (a Mall?) , then shouldn't each
        Lot in the collection be serialized independently?

        <Mall>
        <Lot>
        <ParkedCar>
        <Make>Ijbnxnwhn a</Make>
        <Year>1995</Year>
        <Color>red</Color>
        <VIN>5829496467 92293965529740</VIN>
        </ParkedCar>
        <ParkedMotorcyc le>
        <Make>Ggikttsgp p</Make>
        <Year>1985</Year>
        <Color>red</Color>
        </ParkedMotorcycl e>
        </Lot>
        <Lot>
        <ParkedMotorcyc le>
        <Make>Dpdiqdcce z</Make>
        <Year>1986</Year>
        <Color>green</Color>
        </ParkedMotorcycl e>
        <ParkedCar>
        <Make>Gbulediyj e</Make>
        <Year>1998</Year>
        <Color>blue</Color>
        <VIN>3314567350 41360460902400</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>Dgvpadixc x</Make>
        <Year>1998</Year>
        <Color>blue</Color>
        <VIN>0455094077 86350954614071</VIN>
        </ParkedCar>
        <ParkedMotorcyc le>
        <Make>Htvlwawau a</Make>
        <Year>1985</Year>
        <Color>blue</Color>
        </ParkedMotorcycl e>
        </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>Dgvpadixc x</Make>
        <Year>1998</Year>
        <Color>blue</Color>
        <VIN>0455094077 86350954614071</VIN>
        <Make>Htvlwawau a</Make>
        <Year>1985</Year>
        <Color>blue</Color>
        </ParkingLot>


        What does that get you?

        -D



        "Daniel Lidström" <someone@micros oft.com> wrote in message
        news:1h8iz8q7a9 vpk.p9t7150yckm 5.dlg@40tude.ne t...[color=blue]
        > No answer...
        >
        > Let me describe what I am doing. I've created these classes:
        > [XmlInclude(type of(Car))]
        > [XmlInclude(type of(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(Elem entName="Parked Car",Type=typeo f(Car))]
        > [XmlElement(Elem entName="Parked Motorcycle",Typ e=typeof(Motorc ycle))]
        > 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>
        > <ParkedMotorcyc le>
        > <Year>0</Year>
        > </ParkedMotorcycl e>
        > </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 ParkingLotColle ction;
        > 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(Ty pe=typeof(Parki ngLot))]
        > public ArrayList ParkingLotColle ction;
        >
        > But this creates a parent element around my ParkingLot's, like this:
        > <ParkingLotColl ection>
        > <ParkingLot>
        > <ParkedCar>
        > <Year>0</Year>
        > </ParkedCar>
        > <ParkedMotorcyc le>
        > <Year>0</Year>
        > </ParkedMotorcycl e>
        > </ParkingLot>
        > </ParkingLotColle ction>
        >
        > From what I've read I should be able to remove this parent element by
        > adding a empty XmlElement attribute to my ArrayList ParkingLotColle ction:
        >
        > [XmlArrayItem(Ty pe=typeof(Parki ngLot))]
        > [XmlElement]
        > public ArrayList ParkingLotColle ction;
        >
        > But this causes an exception:
        > Message: There was an error reflecting field 'ParkingLotColl ection'.
        > I have no idea what to do. What am I doing wrong? Any information is very
        > much appreciated. Thanks.
        >
        > --
        > Daniel[/color]


        Comment

        • Dino Chiesa [Microsoft]

          #5
          Re: Problem with collection classes

          I don't understand.
          If a class contains a ParkingLotColle ction (a Mall?) , then shouldn't each
          Lot in the collection be serialized independently?

          <Mall>
          <Lot>
          <ParkedCar>
          <Make>Ijbnxnwhn a</Make>
          <Year>1995</Year>
          <Color>red</Color>
          <VIN>5829496467 92293965529740</VIN>
          </ParkedCar>
          <ParkedMotorcyc le>
          <Make>Ggikttsgp p</Make>
          <Year>1985</Year>
          <Color>red</Color>
          </ParkedMotorcycl e>
          </Lot>
          <Lot>
          <ParkedMotorcyc le>
          <Make>Dpdiqdcce z</Make>
          <Year>1986</Year>
          <Color>green</Color>
          </ParkedMotorcycl e>
          <ParkedCar>
          <Make>Gbulediyj e</Make>
          <Year>1998</Year>
          <Color>blue</Color>
          <VIN>3314567350 41360460902400</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>Dgvpadixc x</Make>
          <Year>1998</Year>
          <Color>blue</Color>
          <VIN>0455094077 86350954614071</VIN>
          </ParkedCar>
          <ParkedMotorcyc le>
          <Make>Htvlwawau a</Make>
          <Year>1985</Year>
          <Color>blue</Color>
          </ParkedMotorcycl e>
          </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>Dgvpadixc x</Make>
          <Year>1998</Year>
          <Color>blue</Color>
          <VIN>0455094077 86350954614071</VIN>
          <Make>Htvlwawau a</Make>
          <Year>1985</Year>
          <Color>blue</Color>
          </ParkingLot>


          What does that get you?

          -D



          "Daniel Lidström" <someone@micros oft.com> wrote in message
          news:1h8iz8q7a9 vpk.p9t7150yckm 5.dlg@40tude.ne t...[color=blue]
          > No answer...
          >
          > Let me describe what I am doing. I've created these classes:
          > [XmlInclude(type of(Car))]
          > [XmlInclude(type of(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(Elem entName="Parked Car",Type=typeo f(Car))]
          > [XmlElement(Elem entName="Parked Motorcycle",Typ e=typeof(Motorc ycle))]
          > 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>
          > <ParkedMotorcyc le>
          > <Year>0</Year>
          > </ParkedMotorcycl e>
          > </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 ParkingLotColle ction;
          > 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(Ty pe=typeof(Parki ngLot))]
          > public ArrayList ParkingLotColle ction;
          >
          > But this creates a parent element around my ParkingLot's, like this:
          > <ParkingLotColl ection>
          > <ParkingLot>
          > <ParkedCar>
          > <Year>0</Year>
          > </ParkedCar>
          > <ParkedMotorcyc le>
          > <Year>0</Year>
          > </ParkedMotorcycl e>
          > </ParkingLot>
          > </ParkingLotColle ction>
          >
          > From what I've read I should be able to remove this parent element by
          > adding a empty XmlElement attribute to my ArrayList ParkingLotColle ction:
          >
          > [XmlArrayItem(Ty pe=typeof(Parki ngLot))]
          > [XmlElement]
          > public ArrayList ParkingLotColle ction;
          >
          > But this causes an exception:
          > Message: There was an error reflecting field 'ParkingLotColl ection'.
          > I have no idea what to do. What am I doing wrong? Any information is very
          > much appreciated. Thanks.
          >
          > --
          > Daniel[/color]


          Comment

          • Daniel Lidström

            #6
            Re: Problem with collection classes

            On Fri, 25 Jun 2004 14:12:25 -0400, Dino Chiesa [Microsoft] wrote:
            [color=blue]
            > I don't understand.
            > If a class contains a ParkingLotColle ction (a Mall?) , then shouldn't each
            > Lot in the collection be serialized independently?
            >
            > <Mall>
            > <Lot>
            > <ParkedCar>
            > <Make>Ijbnxnwhn a</Make>
            > <Year>1995</Year>
            > <Color>red</Color>
            > <VIN>5829496467 92293965529740</VIN>
            > </ParkedCar>
            > <ParkedMotorcyc le>
            > <Make>Ggikttsgp p</Make>
            > <Year>1985</Year>
            > <Color>red</Color>
            > </ParkedMotorcycl e>
            > </Lot>
            > <Lot>
            > <ParkedMotorcyc le>
            > <Make>Dpdiqdcce z</Make>
            > <Year>1986</Year>
            > <Color>green</Color>
            > </ParkedMotorcycl e>
            > <ParkedCar>
            > <Make>Gbulediyj e</Make>
            > <Year>1998</Year>
            > <Color>blue</Color>
            > <VIN>3314567350 41360460902400</VIN>
            > </ParkedCar>
            > </Lot>
            > </Mall>
            >
            >
            > What would you like to see happen?[/color]

            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):


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



            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:


            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

            Comment

            • Daniel Lidström

              #7
              Re: Problem with collection classes

              On Fri, 25 Jun 2004 14:12:25 -0400, Dino Chiesa [Microsoft] wrote:
              [color=blue]
              > I don't understand.
              > If a class contains a ParkingLotColle ction (a Mall?) , then shouldn't each
              > Lot in the collection be serialized independently?
              >
              > <Mall>
              > <Lot>
              > <ParkedCar>
              > <Make>Ijbnxnwhn a</Make>
              > <Year>1995</Year>
              > <Color>red</Color>
              > <VIN>5829496467 92293965529740</VIN>
              > </ParkedCar>
              > <ParkedMotorcyc le>
              > <Make>Ggikttsgp p</Make>
              > <Year>1985</Year>
              > <Color>red</Color>
              > </ParkedMotorcycl e>
              > </Lot>
              > <Lot>
              > <ParkedMotorcyc le>
              > <Make>Dpdiqdcce z</Make>
              > <Year>1986</Year>
              > <Color>green</Color>
              > </ParkedMotorcycl e>
              > <ParkedCar>
              > <Make>Gbulediyj e</Make>
              > <Year>1998</Year>
              > <Color>blue</Color>
              > <VIN>3314567350 41360460902400</VIN>
              > </ParkedCar>
              > </Lot>
              > </Mall>
              >
              >
              > What would you like to see happen?[/color]

              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):


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



              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:


              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

              Comment

              Working...