我的方法中的参数使用什么数据类型来接受XML字符串?

本文关键字:XML 字符串 数据类型 什么 方法 参数 我的 | 更新日期: 2023-09-27 18:04:39

作为我的第一个WCF项目,我感到困惑,需要一些澄清。我想使用WCF来构建一个接受SOAP协议内XML字符串的web服务。

<ITEM_SEND xml:lang="en-US">
   <T_ID>1368</T_ID>
   <PART>8058</PART>
</ITEM_SEND>

所以,我认为我需要为我的接口做以下事情:

[ServiceContract]
public interface IInvService
{
    [OperationContract]
    XmlDocument GetInventory(XmlDocument query);
}

那么对于我的实际方法,我相信我会这样做:

public XmlDocument GetInventory(XmlDocument query)
{   
    XmlDocument xmlDoc = new XmlDocument();    
    //... do stuff and return xml
    return xmlDoc;
}

我是否在使用XmlDocument类型的正确轨道上,或者是否有另一种数据类型,我应该使用的XML字符串?

我的方法中的参数使用什么数据类型来接受XML字符串?

发送和返回XmlDocument有点多余。WCF默认使用SOAP, basicHttpBinding, wsHttpBinding, netttcpbinding等都使用SOAP。我会让框架为你做序列化。

相反,我将创建一个DataContract或使用一个强类型对象。

例如,您的DataContract可能如下所示:
[DataContract]
public class ItemSend
{
    private int _id;
    private int _part;
    [DataMember]
    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
    [DataMember]
    public int Part
    {
        get { return _part; }
        set { _part = value; }
    }
}

您的ServiceContract可能如下所示:

[ServiceContract]
public interface IInvService
{
    [OperationContract]
    ItemSend GetInventory(XmlDocument query);
}

我不确定"查询"XML是什么,但它也可能是一个数据合同。

这里有一个关于数据合约的教程:http://wcftutorial.net/data-contract.aspx