[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()
-
2WHAT error are you getting?!?! We can't read your screen nor your mind - you'll have to tell us!marc_s– marc_s2013年04月03日 06:22:29 +00:00Commented Apr 3, 2013 at 6:22
-
Why a multy diamentol arry and not just serialize objects?CodeCaster– CodeCaster2013年04月03日 06:23:48 +00:00Commented Apr 3, 2013 at 6:23
-
1no im using KSOP2, i dont know how to call for web services using JSONGayashan– Gayashan2013年04月03日 06:24:56 +00:00Commented Apr 3, 2013 at 6:24
-
1I meant why do you want to use arrays, when you can easily serialize objects?CodeCaster– CodeCaster2013年04月03日 06:28:40 +00:00Commented Apr 3, 2013 at 6:28
-
1@codeCaster can you please how do i do it?Gayashan– Gayashan2013年04月03日 06:40:37 +00:00Commented Apr 3, 2013 at 6:40
3 Answers 3
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;
}
-
Invalid rank specifier: expected ',' or ']' im getting this error when i changed code to String[,] xx = new String[arrayZise][2];Gayashan– Gayashan2013年04月03日 06:28:14 +00:00Commented Apr 3, 2013 at 6:28
-
1@Gayashan, my bad, forgot to modify the declaration it should be
String[][] xx
, edited my answerHabib– Habib2013年04月03日 06:29:22 +00:00Commented Apr 3, 2013 at 6:29
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.
-
1I dont know any other ways, if you know some other way can you help me?Gayashan– Gayashan2013年04月03日 06:29:55 +00:00Commented Apr 3, 2013 at 6:29
-
1Well, 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 methodAlex– Alex2013年04月03日 06:33:02 +00:00Commented Apr 3, 2013 at 6:33
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.