2013-04-03 27 views
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; 
    } 

我想从这个Web服务返回一个多维数组我怎么做?如何从Web服务方法返回多维数组?

该代码给出了一个错误

其实这个Web服务从Android应用程序,这就是为什么我回到这个数据作为一个多维数组..

错误通话是:

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() 
+2

**你有什么**错误?!?!我们无法阅读您的屏幕或您的想法 - 您必须**告诉我们!** –

+0

为什么多层隔音板不仅仅是序列化对象? – CodeCaster

+1

没有我使用KSOP2,我不知道如何使用JSON调用Web服务 – Gayashan

回答

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; 
} 
+0

无效排名说明符:预期','或']' 即时更改代码为 字符串[,] xx = new String [arrayZise] [2]; – Gayashan

+1

@Gayashan,我的坏,忘记修改声明它应该是'String [] [] xx',编辑我的答案 – Habib

1

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

+1

我不知道任何其他方式, 如果你知道一些其他方式可以帮助我吗? – Gayashan

+1

好吧,互联网上有很多例子。请参阅http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dfd587ec-a269-49a9-a1a5-0f69d915c776/,以了解如何制作自己的序列化程序,或者只需更改String [,]到String [] []并在你的方法中返回它 – Alex

0

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