返回的WebMethod泛型列表 [英] WebMethod returning generic list

查看:206
本文介绍了返回的WebMethod泛型列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个简单的类:一个订单对象,其中包含的OrderLine对象的列表:

 公共类订单
{
    公共字符串OrderNo {搞定;组; }
    公共字符串客户名称{搞定;组; }    公开名单<&OrderLine的GT;行{搞定;组; }
}公共类订单行
{
    公共字符串货号{搞定;组; }
    公众诠释数量{搞定;组; }
    公共十进制价格{搞定;组; }
}[的WebMethod]
公共秩序GetOrder(字符串orderNo)
{
    返回null;
}[的WebMethod]
公开名单<排序> GetOrderList(字符串orderNo)
{
    返回null;
}

问题在于OrderLine的物体在订单细节(属性)不是由第二的WebMethod呈现(正常工作与第一的WebMethod):

第一的WebMethod(返回一个数量级),这使得XML(正常,与产品货号,数量和放大器;价格为每个订单行):

 < GetOrderResult>
  < OrderNo>串LT; / OrderNo>
  <客户名称>串LT; /客户名称>
  <线条和GT;
    <&OrderLine的GT;
      <货号>串LT; /货号>
      <&数量GT;&LT INT; /数量>
      <价格>&小数LT; /价格>
    < /订单行>
    <&OrderLine的GT;
      <货号>串LT; /货号>
      <&数量GT;&LT INT; /数量>
      <价格>&小数LT; /价格>
    < /订单行>
  < /线>
< / GetOrderResult>

第二的WebMethod(返回一个列表),呈现此XML(注意OrderLines不再与细节渲染):

 < GetOrderListResult>
  <排序>
    < OrderNo>串LT; / OrderNo>
    <客户名称>串LT; /客户名称>
    <线条和GT;
      <订单行XSI:零=真/>
      <订单行XSI:零=真/>
    < /线>
  < /排序>
  <排序>
    < OrderNo>串LT; / OrderNo>
    <客户名称>串LT; /客户名称>
    <线条和GT;
      <订单行XSI:零=真/>
      <订单行XSI:零=真/>
    < /线>
  < /排序>
< / GetOrderListResult>

我怎样才能获得OrderLines与细节而不是作为XSI渲染:无=真正的??

感谢。


感谢您的答复,但这并不能帮助。在code我张贴简化尽可能;当然真正的code实例列表等,但仍具有相同的问题。但我还是想你的样品中建议:

 公共类订单
{
    公共字符串OrderNo {搞定;组; }
    公共字符串客户名称{搞定;组; }    公开名单<&OrderLine的GT;行{搞定;组; }    公共秩序()
    {
        行=新的List<&订单行GT;();
        Lines.Add(新订单行());
        Lines.Add(新订单行());
    }
}

本的WebMethod仍返回:

 < GetOrderListResult>
    <排序>
      < OrderNo>串LT; / OrderNo>
      <客户名称>串LT; /客户名称>
      <线条和GT;
        <订单行XSI:零=真/>
        <订单行XSI:零=真/>
      < /线>
    < /排序>
    <排序>
      < OrderNo>串LT; / OrderNo>
      <客户名称>串LT; /客户名称>
      <线条和GT;
        <订单行XSI:零=真/>
        <订单行XSI:零=真/>
      < /线>
    < /排序>
< / GetOrderListResult>


解决方案

您正在运行到的问题是,一个列表不XMLSerializable(见的这里)。您需要将其转换为东西是如数组。

您可以只使用数组或者您也可以继续使用列表中,但再使用,当你返回值的.ToArray()方法。

请参阅这个的,如果你真的想序列化,而不是使用数组。

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>
        <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>

解决方案

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.

这篇关于返回的WebMethod泛型列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆