Serialization of a custom collection

  • Thread starter Thread starter TarTar
  • Start date Start date
T

TarTar

Hello,

I've created a custom collection:

[XmlRoot("Cars")]
public class CarCollection : List<Car>
{
private string errMsg;

[XmlElement("ErrorMessage")]
public string ErrorMessage { get { return errMsg; } set { errMsg =
value; } }

// some CarCollection-specific methods
// ...
}

[Serializable]
public struct Car
{
public string Model;
public string Make;

public Car(string model, string make)
{
this.Model = model;
this.Make = make;
}
}

.... and a Web Service method:

[WebMethod]
public CarCollection SearchCars()
{
CarCollection cars = GetCars();
cars.ErrorMessage = "Testing, testing...";
return cars;
}

When I call the web method it returns a collection of cars, but the
ErrorMessage element is missing.

How to properly enhance my custom collection? I'd like to serialize some
additional information such as ErrorMessage besides the collection itself.

I appreciate any help,

Leszek
 
Hello,

I've created a custom collection:

    [XmlRoot("Cars")]
    public class CarCollection : List<Car>
    {
        private string errMsg;

        [XmlElement("ErrorMessage")]
        public string ErrorMessage { get { return errMsg; } set {errMsg =
value; } }

        // some CarCollection-specific methods
        // ...
    }

    [Serializable]
    public struct Car
    {
        public string Model;
        public string Make;

        public Car(string model, string make)
        {
            this.Model = model;
            this.Make = make;
        }
    }

... and a Web Service method:

        [WebMethod]
        public CarCollection SearchCars()
        {
            CarCollection cars = GetCars();
            cars.ErrorMessage = "Testing, testing...";
            return cars;
        }

When I call the web method it returns a collection of cars, but the
ErrorMessage element is missing.

How to properly enhance my custom collection? I'd like to serialize some
additional information such as ErrorMessage besides the collection itself..

I do not think it is possible to serialize additional info from the
collection object itself, but it is possible to insert an intermediate
object into the chain that would contain the collection itself
(expanded inline), and any additional info. Something like this:

[XmlRoot("Cars")]
public class Cars
{
[XmlElement] // this will expand the collection elements inline
within <Cars> as individual <Car> elements, without any additional
wrapping element such as <CarCollection>
public List<Car> CarCollection;

[XmlElement]
public string ErrorMessage;

...
}
 
Thanks for your help Pavel. Yeah, it seems I'll have to create a "wrapper"
around my collection. This way I'll be able to provide my additional info
within the wrapper.

Leszek

Hello,

I've created a custom collection:

[XmlRoot("Cars")]
public class CarCollection : List<Car>
{
private string errMsg;

[XmlElement("ErrorMessage")]
public string ErrorMessage { get { return errMsg; } set { errMsg =
value; } }

// some CarCollection-specific methods
// ...
}

[Serializable]
public struct Car
{
public string Model;
public string Make;

public Car(string model, string make)
{
this.Model = model;
this.Make = make;
}
}

... and a Web Service method:

[WebMethod]
public CarCollection SearchCars()
{
CarCollection cars = GetCars();
cars.ErrorMessage = "Testing, testing...";
return cars;
}

When I call the web method it returns a collection of cars, but the
ErrorMessage element is missing.

How to properly enhance my custom collection? I'd like to serialize some
additional information such as ErrorMessage besides the collection itself.

I do not think it is possible to serialize additional info from the
collection object itself, but it is possible to insert an intermediate
object into the chain that would contain the collection itself
(expanded inline), and any additional info. Something like this:

[XmlRoot("Cars")]
public class Cars
{
[XmlElement] // this will expand the collection elements inline
within <Cars> as individual <Car> elements, without any additional
wrapping element such as <CarCollection>
public List<Car> CarCollection;

[XmlElement]
public string ErrorMessage;

...
}
 
I'm not sure if this is still a 2.0 issue, but you might want to check this
1.1 centric issue with CustomCollections and XmlSerialization:

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!114.entry


XmlSerialization with IDictionary and CollectionBase Objects


Leszek said:
Thanks for your help Pavel. Yeah, it seems I'll have to create a "wrapper"
around my collection. This way I'll be able to provide my additional info
within the wrapper.

Leszek

Hello,

I've created a custom collection:

[XmlRoot("Cars")]
public class CarCollection : List<Car>
{
private string errMsg;

[XmlElement("ErrorMessage")]
public string ErrorMessage { get { return errMsg; } set { errMsg =
value; } }

// some CarCollection-specific methods
// ...
}

[Serializable]
public struct Car
{
public string Model;
public string Make;

public Car(string model, string make)
{
this.Model = model;
this.Make = make;
}
}

... and a Web Service method:

[WebMethod]
public CarCollection SearchCars()
{
CarCollection cars = GetCars();
cars.ErrorMessage = "Testing, testing...";
return cars;
}

When I call the web method it returns a collection of cars, but the
ErrorMessage element is missing.

How to properly enhance my custom collection? I'd like to serialize some
additional information such as ErrorMessage besides the collection
itself.

I do not think it is possible to serialize additional info from the
collection object itself, but it is possible to insert an intermediate
object into the chain that would contain the collection itself
(expanded inline), and any additional info. Something like this:

[XmlRoot("Cars")]
public class Cars
{
[XmlElement] // this will expand the collection elements inline
within <Cars> as individual <Car> elements, without any additional
wrapping element such as <CarCollection>
public List<Car> CarCollection;

[XmlElement]
public string ErrorMessage;

...
}
 
Back
Top