c# - XML 序列化以将请求发送到 SOAP 服务 C#

标签 c# xml soap xsd

我有一项服务需要以 XML 格式发送请求。

我有一个 xsd,我使用 xsd.exe 工具生成一个类,该类会自动创建 xmlattributes。

但是我需要填充这个类,但我没有快乐。所以我想填充类中的属性,然后通过请求将其发送到 SOAP 服务。

该类的示例如下所示。由于隐私原因,我只显示了部分信息。

public partial class Request {

    private string[] itemsField;

    private ItemsChoiceType[] itemsElementNameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Name", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("Address1", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("Town", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("County", typeof(string), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]

 [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
    public string[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemsChoiceType[] ItemsElementName {
        get {
            return this.itemsElementNameField;
        }
        set {
            this.itemsElementNameField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema=false)]
public enum ItemsChoiceType {

    /// <remarks/>
    Name,

    /// <remarks/>
    Address1,

    /// <remarks/>
    Town,

    /// <remarks/>
    County
}

如何填充类并使用 xmlserializer 将请求发送到服务。

提前致谢

问候

TJ

最佳答案

How can i populate the class

与任何普通的 .NET 类一样,您可以首先创建它的实例,然后在此实例上设置属性值:

var request = new Request();
request.Items = new[] { "item1", "item2" };
request.ItemsElementName = new[]
{
    ItemsChoiceType.Name,
    ItemsChoiceType.Address1,
};

and use xmlserializer to send the request to the service.

现在这是棘手的部分。首先,XmlSerializer 类,顾名思义,可用于序列化为 XML,而不是发送请求。其次,XmlSerializer 将不会生成 SOAP 服务所需的 SOAP 信封。除了请求 XML 之外,SOAP 信封还包含有关要调用哪个方法的信息。

我建议您使用较新的 svcutil.exe为了从 WSDL 创建 C# 客户端契约:

svcutil.exe http://someservice.com/?WSDL

这将使用更新的 DataContract 属性,您可以使用 WCF 客户端发送请求。这还将创建可用于调用远程服务的 ServiceContract 接口(interface):

[System.ServiceModel.ServiceContract(Namespace="http://service.namespace")]
public interface IMyService 
{
    [System.ServiceModel.OperationContract(Action="http://service.namespace/SomeServiceMethod", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormat(SupportFaults=true)]
    void SomeServiceMethod(Request request);
}

显然,在此示例中,您需要替换为 SOAP 服务的实际 namespace 和正确的操作名称。

最后您可以调用该操作:

var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://example.com/myservice");
var channelFactory = new ChannelFactory<IMyService>(binding, endpoint);
var client = channelFactory.CreateChannel();
client.SomeServiceMethod(request);

关于c# - XML 序列化以将请求发送到 SOAP 服务 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36820100/

相关文章:

xml - 为什么微软匹配一个XSD xs :integer to a string when importing WSDL?

soap - 使用 Angular 2 创建 SOAP 客户端

java - Eclipse 中的 WSDL 验证命名空间失败

c# - MVC 5 中的身份验证过滤器

c# - 构建一个 "scriptable"应用程序

java - 使用属性获取元素

java - Spring @AutoWired 始终为 null

c# - 在 Unity 中使用事件触发器 SetGazedAt 进行 Lerping 颜色

c# - Convert.ToInt32 - 保持前导零

用于处理具有多个 XML 模式的消息的 Java 模式