Serialization of a custom collection

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
 
P

Pavel Minaev

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;

...
}
 
L

Leszek

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;

...
}
 
S

sloan

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;

...
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top