Привет, мне нужно вернуть 2D-массив из веб-службы ASP.NET.
Сначала я попробовал это решение:
[WebMethod]
public string[,] ReturnMultiDimArray()
{
var x = new string[,] { { "ab" }, { "cd" } };
return x;
}
Я получил ошибку:
Невозможно сериализовать объект типа System.String [,]. Многомерные массивы не поддерживаются.
Это нормально, поэтому я попробовал вот так.
[WebMethod]
public string[][] ReturnMultiDimArray()
{
string[] y = { "ab", "cd" };
string[] z = { "ef", "gh" };
string[][] x = { y, z };
return x;
}
Я получил эту ошибку:
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidCastException: Unable to cast object of type 'System.String[][]' to type 'System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.String]]'.
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write5_ArrayOfArrayOfString(Object o)
at Microsoft.Xml.Serialization.GeneratedAssembly.ListOfListOfStringSerializer4.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()
Как я могу сериализовать "2D массив"? Мне нужно вернуться из веб-метода "2D массив".