简体   繁体   中英

How to return a multi-dimensional array from a web service method?

[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;
    }

I want to return a multi-dimensional array from this web service how do I do it?

This code gives a error

Actually this web service is calling from android application that's why I return this data as a multi-dimensional array..

Error 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()

Based on your previous question and the errror you specify in comments under the answer, I believe you should return a Jagged Array something like:

[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;
 }

You have the answer in your question exception - Multidimensional arrays are not supported. at System.Xml.Serialization. You have to return it in some different way - Jagged Array or write your own Serializer.

I had the same problem, a year later but I looked for several solutions and if you want to return JSON or XML then you just need to invoke a serializer. I created a Dictionary object and was returning this. It worked fine in a web page but not for a web service. So after a little hunting I found this Json Object serializer. I just had to pass my object into JsonConvert(SerializeObject) and whalla JSON comes back and the web service works! This is a terrific package: http://james.newtonking.com/json .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM