user278618 user278618 - 1 month ago 9
C# Question

...may not be used in this context...while serialization

I've webservice and WebMethod

[webMethod]
[XMLInclude(typeof(ContractCar[]))
public object GetObjects(Cars[] cars)
{
return Translator.ToObjects(Facade.GetObjects(cars);

}

public static object GetObjects(Cars cars)
{
List<Car> cars =new List<Country(...fillingcollection)
return cars.ToArray(),
}

public static object ToObjects(object collection)
{
if(collection is Car[])
{
return ConvertModelCarsToContractCars(collection),
}

public ContractCar[] ConvertModelCarsToContractCars(Cars[] collection)
{
...there is rewriting pool...
}


And I get exception at side of client:

There was an error generating the XML document.

I'm using this function to check collection which I would send to the client and it works.

public static void SerializeContainer(object obj)
{
try
{
// Make sure even the construsctor runs inside a
// try-catch block
XmlSerializer ser = new XmlSerializer(typeof(object));

TextWriter w = new StreamWriter(@"c:\list.xml");


ser.Serialize(w, obj);
w.Close();

}
catch (Exception ex)
{
DumpException(ex);
}
}


Interesting is when collection has only One element [webmethod] works fine, but when is more it brokes

Answer

I changed object into objects[].