求教在Delphi7中调用C# Web Service的方法(函数返回值类型为DataSet)!

shenjing 2004-01-03 04:50:51
本人认为用C#开发Web Service服务器端简单,易用.
而客户端用Delphi设计界面美观

在Delphi7中我可以成功调用C# Web Service的方法中返回类型为int, string等,唯一调用类型为DataSet的总无法成功取出其记录值.

恳盼大侠指点一二!
谢谢!!!
...全文
298 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
serf 2004-08-23
  • 打赏
  • 举报
回复
我测试过bcb,当server端返回DataSet时,生成的代理程序根本不填充那个接收字段。如果你要这么做,除非你能改写服务端,返回型统一用string. 上面这位朋友很强,不过我不推荐这么做,因为这么做,很难考虑周全,稍有不慎就会出错,ADODB.RecorderSet能读取xml流,生成表。然后操作就简单了。要求就是该dataset只有一个表。
wzs_wzs 2004-07-01
  • 打赏
  • 举报
回复
以下是在.NET中调用上边的XSL文件对Dataset产生的XML进行转换的C#代码(转换比较耗时,我们现在已经放在客户端使用Delphi进行转换了):

protected string ConverXML(string sXML)
{
string sResult = "";
try
{
//加载XSL
System.Xml.XmlTextReader myXslReader = new System.Xml.XmlTextReader(this.getXSLTString(),System.Xml.XmlNodeType.Document,null);
System.Xml.Xsl.XslTransform myXsl = new System.Xml.Xsl.XslTransform();
myXsl.Load(myXslReader);

//加载需要转换的.NET XML System.Xml.XmlTextReader myXmlReader = new System.Xml.XmlTextReader(sXML,System.Xml.XmlNodeType.Document,null);
System.Xml.XmlDocument myXMLData = new System.Xml.XmlDocument();
myXMLData.Load(myXmlReader);

//转换
System.IO.MemoryStream myMS = new System.IO.MemoryStream();
myXsl.Transform(myXMLData,null,myMS);
System.IO.StreamReader mySR = new System.IO.StreamReader(myMS);
mySR.BaseStream.Seek(0,System.IO.SeekOrigin.Begin);
sResult = mySR.ReadToEnd();
myMS.Close();
mySR.Close();
}
catch(Exception myExce)
{
throw myExce;
}
finally
{
}
return sResult;
}
wzs_wzs 2004-07-01
  • 打赏
  • 举报
回复
.NET 的DataSet转换为Delphi的Dataset(自己做的,供大家参考),下边是XSL文件的代码:<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts">
<xsl:output method="xml" indent="yes"/>
<msxsl:script language="C#" implements-prefix="user">
<![CDATA[
System.Collections.ArrayList myDateTime = new System.Collections.ArrayList();
System.Collections.ArrayList myColumes = new System.Collections.ArrayList();
private int iIndex = 0;

public string getCurName()
{
string sName = "c" + iIndex.ToString();
iIndex ++;
return sName;
}

public void AddColumn(string sColumn)
{
myColumes.Add(sColumn);
}

public string getNameIndex(string sColume)
{
for(int i = 0 ; i < myColumes.Count ; i++)
{
string sName = myColumes[i].ToString();
if(sColume.Equals(sName))
{
return "c" + i.ToString();
}
}
return sColume;
}

public void AddDateTime(string sName)
{
myDateTime.Add(sName);
}

public string ConverMark(string sValue)
{
sValue = sValue.Replace("<","<");
sValue = sValue.Replace(">",">");
sValue = sValue.Replace("\"",""");
return sValue;
}

public string ConverName(string sName)
{
try
{
sName = System.Xml.XmlConvert.DecodeName(sName);
}
catch
{
}
return sName;
}

public string ConverDateTimeLenght(string sName,string sValue)
{
int iLength = 19;
for(int i = 0 ; i < myDateTime.Count ; i++)
{
if(sName.Equals(myDateTime[i].ToString()))
{
if(sValue.Length > iLength)
{
sValue = sValue.Substring(0,iLength);
}
break;
}
}

sValue = ConverMark(sValue);
return sValue;
}
]]>
</msxsl:script>

<xsl:template match="/NewDataSet">

<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'
xmlns:rs='urn:schemas-microsoft-com:rowset'
xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'>
<s:ElementType name='row' content='eltOnly' rs:updatable='true'>

<xsl:for-each select="xs:schema/xs:element/xs:complexType/xs:choice/xs:element/xs:complexType/xs:sequence/*">

<xsl:element name="s:AttributeType">

<xsl:attribute name="name">
<xsl:value-of select="user:getCurName()"/>
</xsl:attribute>

<xsl:attribute name="rs:name">
<xsl:value-of select="user:AddColumn(@name)"/>
<xsl:value-of select="user:ConverName(@name)"/>
</xsl:attribute>


<xsl:element name="s:datatype">
<xsl:attribute name="dt:type">
<xsl:choose>
<xsl:when test="xs:simpleType/xs:restriction/@base != ''">
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:int'">int</xsl:if>
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:unsignedbyte'">ui1</xsl:if>
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:dateTime'">dateTime</xsl:if>
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:decimal'">number</xsl:if>
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:boolean'">boolean</xsl:if>
<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:string'">string</xsl:if>
</xsl:when>
<xsl:otherwise>
<xsl:if test="@type = 'xs:int'">int</xsl:if>
<xsl:if test="@type = 'xs:unsignedbyte'">ui1</xsl:if>
<xsl:if test="@type = 'xs:dateTime'">dateTime</xsl:if>
<xsl:if test="@type = 'xs:decimal'">number</xsl:if>
<xsl:if test="@type = 'xs:boolean'">boolean</xsl:if>
<xsl:if test="@type = 'xs:string'">string</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:attribute name="dt:maxLength">
<xsl:choose>
<xsl:when test="xs:simpleType/xs:restriction/xs:maxLength/@value != ''">
<xsl:value-of select="xs:simpleType/xs:restriction/xs:maxLength/@value"/>
</xsl:when>
<xsl:otherwise>
<xsl:if test="@type = 'xs:int'">20</xsl:if>
<xsl:if test="@type = 'xs:unsignedbyte'">1</xsl:if>
<xsl:if test="@type = 'xs:dateTime'">16</xsl:if>
<xsl:if test="@type = 'xs:decimal'">20</xsl:if>
<xsl:if test="@type = 'xs:boolean'">2</xsl:if>
<xsl:if test="@type = 'xs:string'">30</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>

<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:decimal'">
<xsl:attribute name="rs:scale">5</xsl:attribute>
<xsl:attribute name="rs:precision">14</xsl:attribute>
</xsl:if>
<xsl:if test="@type = 'xs:decimal'">
<xsl:attribute name="rs:scale">5</xsl:attribute>
<xsl:attribute name="rs:precision">14</xsl:attribute>
</xsl:if>

<xsl:if test="xs:simpleType/xs:restriction/@base = 'xs:dateTime'">
<xsl:value-of select="user:AddDateTime(@name)"/>
<xsl:attribute name="rs:scale">3</xsl:attribute>
<xsl:attribute name="rs:precision">23</xsl:attribute>
<xsl:attribute name="rs:fixedlength">true</xsl:attribute>
</xsl:if>
<xsl:if test="@type = 'xs:dateTime'">
<xsl:value-of select="user:AddDateTime(@name)"/>
<xsl:attribute name="rs:scale">3</xsl:attribute>
<xsl:attribute name="rs:precision">23</xsl:attribute>
<xsl:attribute name="rs:fixedlength">true</xsl:attribute>
</xsl:if>

</xsl:element>
</xsl:element>
</xsl:for-each>

<s:extends type='rs:rowbase'/>
</s:ElementType>
</s:Schema>

<rs:data>
<xsl:for-each select="Table">
<xsl:text disable-output-escaping="yes"><z:row </xsl:text>
<xsl:for-each select="*">
<xsl:text disable-output-escaping="yes"> </xsl:text><xsl:value-of select="user:getNameIndex(name())"/>="<xsl:value-of select="user:ConverDateTimeLenght(name(),.)" disable-output-escaping="yes" />"<xsl:text disable-output-escaping="yes"> </xsl:text>
</xsl:for-each>
<xsl:text disable-output-escaping="yes">/></xsl:text>
</xsl:for-each>
</rs:data>
</xml>
</xsl:template>
</xsl:stylesheet>
jjstar 2004-06-25
  • 打赏
  • 举报
回复
自己分析返回的string,不一定用什么xslt的
bigmingming 2004-06-24
  • 打赏
  • 举报
回复
只能通过XSLT进行转换吗?通过程序行吗?
wzs_wzs 2004-06-17
  • 打赏
  • 举报
回复
使用XSLT进行转换,我们已经成功了。
Piyongcai 2004-06-15
  • 打赏
  • 举报
回复
使用Soap
windblue 2004-06-11
  • 打赏
  • 举报
回复
web service返回的DataSet可以映射成以下这个类
TSoapResultBase = class(TRemotable)
protected
Fschema: WideString;
FData: WideString;
published
property schema: WideString read Fschema write Fschema;
property diffgram: WideString read FData write FData;
end;
schema和diffgram是两个XML字符串序列
其中schema定义了DataSet的结构
diffgram为其数据集
分析这两个XML字符串
将其转换成TClientDataSet使用的xml格式
最后使用ClientDataSet的LoadFromFile就可以了
剩下的就是访问您熟悉的TClientDataSet了
bigmingming 2004-06-11
  • 打赏
  • 举报
回复
真的不行吗?DELPHI中能用TABLE来接收吗?郁闷中...................
rottenapple 2004-01-05
  • 打赏
  • 举报
回复
返回string xml 自己解析把。
如果dataset你真的想用这个,把整个soap包得到,自己解析也可以,不过更麻烦。

我觉得是这样的,如果web service是别人提供的,那么你也不必关心。如果自己提供的,你可以改变web service返回类型。自己选择了
shenjing 2004-01-05
  • 打赏
  • 举报
回复
感谢楼上的各位, 我想也只能将DataSet的数据转换成XML字串
ZXYSOSO 2004-01-05
  • 打赏
  • 举报
回复
用XML吧,XML就可以在异构中传输和解析
inethax 2004-01-04
  • 打赏
  • 举报
回复
是啊
walkinhill 2004-01-04
  • 打赏
  • 举报
回复
这个恐怕没有办法了,Delphi 中根本就没有 DataSet 这个类,你说怎么能直接在 Delphi中使用呢?要真要这么做的话,你可以直接返回 XML 字符串,然后再在 Delhpi中分析 XML。

这也是需要注意的,虽然说 XML Web Services 可以很容易实现异构应用,但是当你使用开发语言不兼容的,你只能自己分析 SOAP 了

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧