WebService 返回 DataTable 问题。

问题描述

发现.netWebService一个怪异的问题,看代码例如:[WebMethod]publicDataTableTest(){DataTabledt=newDataTable("tb1");returndt;}[WebMethod][XmlInclude(typeof(AClass))]publicobjectTest(){AClassc=newAClass();returnc;}

调用正常。[WebMethod]publicobjectTest(){DataTabledt=newDataTable("tb1");returndt;}

调用报错。System.InvalidOperationException:生成XML文档时出错。--->System.InvalidOperationException:类型System.Data.DataTable不能用在此上下文中。要将System.Data.DataTable用作参数、返回类型或者类或结构的成员,该参数、返回类型或成员必须声明为类型System.Data.DataTable(它不能是对象)。类型System.Data.DataTable的对象不能用在非类型化的集合如ArrayList中。在System.Xml.Serialization.XmlSerializationWriter.WriteTypedPrimitive(Stringname,Stringns,Objecto,BooleanxsiType)在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write1_Object(Stringn,Stringns,Objecto,BooleanisNullable,BooleanneedType)在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write17_ServiceResult(Stringn,Stringns,ServiceResulto,BooleanisNullable,BooleanneedType)在Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriter1.Write46_ServiceResult(Objecto)在System.Xml.Serialization.XmlSerializer.Serialize(XmlWriterxmlWriter,Objecto,XmlSerializerNamespacesnamespaces,StringencodingStyle,Stringid)---内部异常堆栈跟踪的结尾---在System.Xml.Serialization.XmlSerializer.Serialize(XmlWriterxmlWriter,Objecto,XmlSerializerNamespacesnamespaces,StringencodingStyle,Stringid)在System.Xml.Serialization.XmlSerializer.Serialize(TextWritertextWriter,Objecto,XmlSerializerNamespacesnamespaces)在System.Web.Services.Protocols.XmlReturnWriter.Write(HttpResponseresponse,StreamoutputStream,ObjectreturnValue)在System.Web.Services.Protocols.WebServiceHandler.WriteReturns(Object[]returnValues)在System.Web.Services.Protocols.WebServiceHandler.Invoke()为什么object为其他可序列化的类型数据都可以返回。有什么办法可以让它可以正常返回DataTable。别让我改方法返回类型。

解决方案

解决方案二:
基于SOAP协议的通讯,是不能使用父类引用的。SOAP协议,会将对对象序列化成XML传输——接收端,再将XML反序列化成对象。如果对象的类型不明确——这段XML将无法完成解析。如果想让你的方法通用:你可以用stringbyte[]——这些基础类型。
解决方案三:
[WebMethod]publicDataTablegetTableData(){SqlConnectionconn=newSqlConnection("server=localhost;database=data;uid=sa;pwd=sa");SqlDataAdapterda=newSqlDataAdapter("select*fromtable",conn);if(conn.State==ConnectionState.Closed){conn.Open();}DataTabledt=newDataTable();da.Fill(dt);if(conn.State==ConnectionState.Open){conn.Close();}returndt;}}
解决方案四:
会存在这个问题,具体原因未研究,可能是序列化的问题,datatable在类中存在时,直接序列化也是失败的。建议通过将表转换为xml文件的方式进行数据传输,如果都是。net开发就很简单了,否则调用端写下xml的解析函数就OKp_DtResult.WriteXml(p_Data_SetInfo.p_strResultPath,XmlWriteMode.WriteSchema);p_DtResult.ReadXml
解决方案五:
引用3楼zixingcheng_的回复:

会存在这个问题,具体原因未研究,可能是序列化的问题,datatable在类中存在时,直接序列化也是失败的。建议通过将表转换为xml文件的方式进行数据传输,如果都是。net开发就很简单了,否则调用端写下xml的解析函数就OKp_DtResult.WriteXml(p_Data_SetInfo.p_strResultPath,XmlWriteMode.WriteSchema);p_DtResult.ReadXml

目前就是这样操作,但是具体原因是什么?有点不能理解。也找不到任何相关资料。

时间: 2024-07-29 01:46:22

WebService 返回 DataTable 问题。的相关文章

android 调用c# webservice 返回datatable

问题描述 如题,这个是返回的字符串结果anyType{schema=anyType{element=anyType{complexType=anyType{choice=anyType{element=anyType{complexType=anyType{sequence=anyType{element=anyType{};element=anyType{};};};};};};};};diffgram=anyType{DocumentElement=anyType{table=anyType

net-C# 调用远程webservice返回的xml 转换为datatable.问题

问题描述 C# 调用远程webservice返回的xml 转换为datatable.问题 返回的xml <?xml version="1.0"?> <Response> <Execution> <Status code="0" sqlcode="0" description="" /> </Execution> <ResponseContent> <

axis-soapui访问Axis2发布的webservice返回异常SAXParseException

问题描述 soapui访问Axis2发布的webservice返回异常SAXParseException 原因是encodingtyle元素not bound,wsdl如下: <?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://192.168.5.191:8088/dataSync/services/MonitorDataLoad

.Net读取Excel 返回DataTable实例代码

 这篇文章主要介绍了.Net读取Excel 返回DataTable实例代码,有需要的朋友可以参考一下 代码如下: using System; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Collections; using System

Java访问WebService返回XML数据的方法

  本文实例讲述了Java访问WebService返回XML数据的方法.分享给大家供大家参考.具体如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

webservice返回xml数据读取 XML 数据时,超出最大字符串内容长度配额 (8192)。

问题描述 webservice返回xml数据读取 XML 数据时,超出最大字符串内容长度配额 (8192). webservice返回xml数据读取 XML 数据时,超出最大字符串内容长度配额 (8192).通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额 ,怎么解决?谢谢了 麻烦带具体的步骤代码 解决方案 读取 XML 数据时,超出最大字符串内容长度配额 (8192).读取 X

webservice 返回json多出一个{&amp;amp;quot;d&amp;amp;quot;:null}

问题描述 webservice返回json多出一个{"d":null},如下{"Response":null,"Code":2,"Message":"工作名称已存在"}{"d":null}后台是这么写的publicvoidReturnMsg(){varresponse=HttpContext.Current.Response;response.ContentType="appl

webservice返回base64Binary内容axis客户端DataHandler接收如何获取内容?

问题描述 webservice返回base64Binary的字符串axis自动生成的客户端使用DataHandler接收如何获取内容?正常应该是返回给我的base64加密字符串,我现在获取出来的都是乱码源码org.apache.axiom.util.stax.XMLStreamReaderUtils.getDataHandlerFromElement(reader)难道是因为使用了Base64DecodingOutputStreamWriter导致的?publicstaticDataHandle

web service-jax-ws调用webService返回结果为json,使用拼装soap调用返回信息转义了

问题描述 jax-ws调用webService返回结果为json,使用拼装soap调用返回信息转义了 返回json:{"loginName":"admin1","msg":"return success","ret":"0"} 以下是调用方法: public static String callWS(String SOAPUrl, String body) throws Exceptio