Re: Serialization of a custom collection



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

"Pavel Minaev" <int19h@xxxxxxxxx> wrote in message
news:79038a0a-5cbb-44f2-b6b9-dea88e0c5046@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On Jan 29, 8:47 am, "TarTar" <spam.s...@xxxxxxxxxxx> wrote:
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;

...
}


.



Relevant Pages

  • Re: Serialization of a custom collection
    ... public class CarCollection: List ... public string Model; ... CarCollection cars = GetCars; ... How to properly enhance my custom collection? ...
    (microsoft.public.dotnet.languages.csharp)
  • Serialization of a custom collection
    ... public class CarCollection: List ... private string errMsg; ... CarCollection cars = GetCars; ... How to properly enhance my custom collection? ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: web service error - urgent
    ... What about this instead (return an array of Cars): ... Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) ... public string make; ...
    (microsoft.public.dotnet.framework.webservices)
  • RE: web service error - urgent
    ... What is "ports" anyway? ... What about this instead (return an array of Cars): ... Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id) ... public string make; ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: Datagrid and switch
    ... public string ConvertFieldType(int fieldTypeId){ ... > If it is a 1 it must show cars. ... > If it is a 2 is must show bikes. ...
    (microsoft.public.dotnet.framework.aspnet)

Loading