WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。

情况是SingleRoom和DoubleRoom是Room类的子类。在WebService中有一个方法是返回Room类。

public Room Get(int roomId)
{
    return Room.Get(roomId);
}

XmlSerializer在序列化时只能识别父类,而不能识别子类,所以导致序列化的错误。需要将[XmlInclude(typeof(SingleRoom))]和[XmlInclude(typeof(DoubleRoom))]同时加到WebService中。

[XmlInclude(typeof(SingleHouse))]
[XmlInclude(typeof(DoubleHouse))]
public class RoomService : System.Web.Services.WebService

 

posted @ 2019-01-20 20:06  1936xinyang  阅读(850)  评论(0编辑  收藏  举报