I have two simple classes: an Order object, which contains a list of OrderLine objects:
public class Order
{
public string OrderNo { get; set; }
public string CustomerName { get; set; }
public List<OrderLine> Lines { get; set; }
}
public class OrderLine
{
public string ItemNo { get; set; }
public int Qty { get; set; }
public decimal Price { get; set; }
}
[WebMethod]
public Order GetOrder(string orderNo)
{
return null;
}
[WebMethod]
public List<Order> GetOrderList(string orderNo)
{
return null;
}
The problem is that the details (properties) of the OrderLine objects in the Orders are not rendered by the second WebMethod (it works fine with the first WebMethod):
The first WebMethod (returning an Order), renders this XML (properly, with ItemNo, Qty & Price for each OrderLine):
<GetOrderResult>
<OrderNo>string</OrderNo>
<CustomerName>string</CustomerName>
<Lines>
<OrderLine>
<ItemNo>string</ItemNo>
<Qty>int</Qty>
<Price>decimal</Price>
</OrderLine>
<OrderLine>
<ItemNo>string</ItemNo>
<Qty>int</Qty>
<Price>decimal</Price>
</OrderLine>
</Lines>
</GetOrderResult>
The second WebMethod (returning a List), renders this XML (note that the OrderLines are no longer rendered with details):
<GetOrderListResult>
<Order>
<OrderNo>string</OrderNo>
<CustomerName>string</CustomerName>
<Lines>
<OrderLine xsi:nil="true" />
<OrderLine xsi:nil="true" />
</Lines>
</Order>
<Order>
<OrderNo>string</OrderNo>
<CustomerName>string</CustomerName>
<Lines>
<OrderLine xsi:nil="true" />
<OrderLine xsi:nil="true" />
</Lines>
</Order>
</GetOrderListResult>
How can I get the OrderLines to render with details instead of as xsi:nil="true"??
Thanks.
Thanks for the reply, but that does not help. The code I posted is simplified as much as possible; the real code of course instantiates the list, etc., but still has the same issue. But I still tried your suggestion in the sample:
public class Order
{
public string OrderNo { get; set; }
public string CustomerName { get; set; }
public List<OrderLine> Lines { get; set; }
public Order()
{
Lines = new List<OrderLine>();
Lines.Add(new OrderLine());
Lines.Add(new OrderLine());
}
}
The WebMethod still returns:
<GetOrderListResult>
<Order>
<OrderNo>string</OrderNo>
<CustomerName>string</CustomerName>
<Lines>
<O开发者_StackOverflow中文版rderLine xsi:nil="true" />
<OrderLine xsi:nil="true" />
</Lines>
</Order>
<Order>
<OrderNo>string</OrderNo>
<CustomerName>string</CustomerName>
<Lines>
<OrderLine xsi:nil="true" />
<OrderLine xsi:nil="true" />
</Lines>
</Order>
</GetOrderListResult>
The problem you are running into is that a List is not XMLSerializable (see here). You will need to convert it to something that is such as an array.
You can either just use an array or you can continue to use the List but then use the .ToArray() method when you are returning the value.
See this if you really want to serialize it and not use the array.
your need to make an instance of the generic List.
You can do this in the constructor
public class Order
{
public string OrderNo { get; set; }
public string CustomerName { get; set; }
public List<OrderLine> Lines { get; set; }
public Order()
{
this.Lines = new List<OrderLine>();
}
}
精彩评论