0
[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()
Freelancer
9,0927 gold badges45 silver badges81 bronze badges
asked Apr 3, 2013 at 6:20
5
  • 2
    WHAT error are you getting?!?! We can't read your screen nor your mind - you'll have to tell us! Commented Apr 3, 2013 at 6:22
  • Why a multy diamentol arry and not just serialize objects? Commented Apr 3, 2013 at 6:23
  • 1
    no im using KSOP2, i dont know how to call for web services using JSON Commented Apr 3, 2013 at 6:24
  • 1
    I meant why do you want to use arrays, when you can easily serialize objects? Commented Apr 3, 2013 at 6:28
  • 1
    @codeCaster can you please how do i do it? Commented Apr 3, 2013 at 6:40

3 Answers 3

2

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;
 }
answered Apr 3, 2013 at 6:23
2
  • Invalid rank specifier: expected ',' or ']' im getting this error when i changed code to String[,] xx = new String[arrayZise][2]; Commented Apr 3, 2013 at 6:28
  • 1
    @Gayashan, my bad, forgot to modify the declaration it should be String[][] xx, edited my answer Commented Apr 3, 2013 at 6:29
1

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.

answered Apr 3, 2013 at 6:27
2
  • 1
    I dont know any other ways, if you know some other way can you help me? Commented Apr 3, 2013 at 6:29
  • 1
    Well, there are many examples over internet. See social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/… so see how to make your own serializer, or simply change your String[,] to String[][] and return it in your method Commented Apr 3, 2013 at 6:33
0

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.

answered Apr 11, 2014 at 16:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.