如何从Web服务方法返回多维数组?

时间:2013-04-03 06:20:39

标签: c# arrays web-services

[WebMethod]
public  Object  GetAllItemsArray() 
{
        FoodCityData.ShoppingBuddyEntities fdContext = new FoodCityData.ShoppingBuddyEntities();

        IQueryable<Item> Query =
       from c in fdContext.Item
       select c;

        List<Item> AllfNames = Query.ToList();
        int arrayZise = AllfNames.Count;
        String[,] xx = new String[arrayZise,2];
        int i = 0;
        int j = 0;
        foreach(Item x in AllfNames)
        {

                xx[i,0] = x.ItemName.ToString();
                xx[i, 1] = x.ItemPrice.ToString();
                i++;
        }

         return (Object)xx;
    }

我想从这个Web服务返回一个多维数组,我该怎么做?

此代码出错

实际上这个Web服务是从android应用程序调用的,这就是我将这些数据作为多维数组返回的原因..

错误IS:

System.InvalidOperationException: There was an error generating the XML document. ---> System.NotSupportedException: Cannot serialize object of type System.String[,]. Multidimensional arrays are not supported.
   at System.Xml.Serialization.TypeDesc.CheckSupported()
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError)
   at System.Xml.Serialization.XmlSerializationWriter.CreateUnknownTypeException(Type type)
   at System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(String name, String ns, Object o, Boolean xsiType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(String n, String ns, Object o, Boolean isNullable, Boolean needType)
   at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_anyType(Object o)
   at Microsoft.Xml.Serialization.GeneratedAssembly.ObjectSerializer.Serialize(Object objectToSerialize, XmlSerializationWriter writer)
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o, XmlSerializerNamespaces namespaces)
   at System.Xml.Serialization.XmlSerializer.Serialize(TextWriter textWriter, Object o)
   at System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponse response, Stream outputStream, Object returnValue)
   at System.Web.Services.Protocols.HttpServerProtocol.WriteReturns(Object[] returnValues, Stream outputStream)
   at System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[] returnValues)
   at System.Web.Services.Protocols.WebServiceHandler.Invoke()

3 个答案:

答案 0 :(得分:2)

根据您的previous question以及您在答案中的评论中指定的错误,我相信您应该返回Jagged Array之类的内容:

[WebMethod]
public  string[][]  GetAllItemsArray() 
{
        FoodCityData.ShoppingBuddyEntities fdContext = new FoodCityData.ShoppingBuddyEntities();

        IQueryable<Item> Query =
       from c in fdContext.Item
       select c;

        List<Item> AllfNames = Query.ToList();
        int arrayZise = AllfNames.Count;
        String[][] xx = new String[arrayZise][2]; //change here
        int i = 0;
        int j = 0;
        foreach(Item x in AllfNames)
        {

                xx[i][0] = x.ItemName.ToString();
                xx[i][1] = x.ItemPrice.ToString();
                i++;
        }

         return xx;
 }

答案 1 :(得分:1)

您的问题答案中有答案 - 不支持多维数组。在System.Xml.Serialization。你必须以某种不同的方式返回它 - Jagged Array或编写你自己的Serializer。

答案 2 :(得分:0)

我有同样的问题,一年后我找了几个解决方案,如果你想返回JSON或XML,那么你只需要调用一个序列化器。我创建了一个Dictionary对象并返回了它。它在网页中运行良好,但不适用于Web服务。经过一点点狩猎后,我找到了这个Json Object序列化器。我只需要将我的对象传递给JsonConvert(SerializeObject)并且whalla JSON回来并且Web服务正常工作!这是一个了不起的包:http://james.newtonking.com/json