I have a Car class and a derivative of SportsCar: Car
Something like that:
public class Car { public int TopSpeed{ get; set; } } public class SportsCar : Car { public string GirlFriend { get; set; } }
I have a web service with methods that return cars ie:
[WebMethod] public Car GetCar() { return new Car() { TopSpeed = 100 }; }
It returns:
<Car> <TopSpeed>100</TopSpeed> </Car>
I have another method that also returns such cars:
[WebMethod] public Car GetMyCar() { Car mycar = new SportsCar() { GirlFriend = "JLo", TopSpeed = 300 }; return mycar; }
It compiles everything and everything, but when I call, I get:
System.InvalidOperationException: An error occurred while generating an XML document. ---> System.InvalidOperationException: Type wsBaseDerived.SportsCar was not expected. Use the XmlInclude or SoapInclude attribute to indicate types that are not known statically.
It seems strange to me that he cannot serialize it as a direct car, since my car is a car.
Adding XmlInclude to WebMethod of our method fixes the error:
[WebMethod] [XmlInclude(typeof(SportsCar))] public Car GetMyCar() { Car mycar = new SportsCar() { GirlFriend = "JLo", TopSpeed = 300 }; return mycar; }
and now it returns:
<Car xsi:type="SportsCar"> <TopSpeed>300</TopSpeed> <GirlFriend>JLo</GirlFriend> </Car>
But I really want to return the base class, without additional properties, etc. from a derived class.
Is this possible without creating maps, etc.?
Say yes;)
c # serialization web-services base
HenriM
source share