2017-07-28 206 views
0

我想集成这个api,但不知道如何在这个XML中传递值作为用户名和密码。如何在xml中传递值作为SOAP请求

<xsd:element name="Login"> 
    <xsd:annotation> 
    <xsd:documentation> 

     </xsd:documentation> 
    </xsd:annotation> 
    <xsd:complexType> 
    <xsd:attribute name="UserName" use="required"> 
     <xsd:simpleType> 
     <xsd:restriction base="xsd:string"> 
      <xsd:length value="50"/> 
      <xsd:minLength value="6"/> 
      <xsd:maxLength value="50"/> 
     </xsd:restriction> 
     </xsd:simpleType> 
    </xsd:attribute> 
    <xsd:attribute name="Password" use="required"> 
     <xsd:simpleType> 
     <xsd:restriction base="xsd:string"> 
      <xsd:length value="255"/> 
      <xsd:minLength value="1"/> 
      <xsd:maxLength value="255"/> 
     </xsd:restriction> 
     </xsd:simpleType> 
    </xsd:attribute> 
    </xsd:complexType> 
</xsd:element> 

文档主页是http://www.e-courier.com/ecourier/software/schema/xmloverview.html#Login。在此先感谢您的时间。

回答

1

您正在查看Login元素的XSD架构。该模式描述了您的请求应该如何。

在您链接看到XML的登录请求是如何塑造了页:

<Login UserName='test' Password='test' /> 

由于该服务是在XML是如何塑造非常挑剔(的确很重要,例如命名空间前缀的命名方式,我认为这是他们的一个实现错误)我使用了XmlSerializer和匹配的DTO对象来实现这个功能。

请注意我如何添加XmlSerializerNamespaces和所需的前缀。

// setup the DTO 
var s = new Envelope { 
    Body = new Body { 
    Login = new Login { 
     UserName = "test" , 
     Password = "test" , 
     WebSite = "ecourier" 
    } 
    } 
}; 

// setup namespaces and their prefixes  
var ns = new XmlSerializerNamespaces(); 
ns.Add("SOAP", "http://schemas.xmlsoap.org/soap/envelope/"); 
ns.Add("m","http://www.e-courier.com/software/schema/public/"); 

// create the serializer 
var ser = new XmlSerializer(typeof(Envelope)); 

using(var ms = new MemoryStream()) 
{ 
    // write the DTO to the MemoryStream 
    ser.Serialize(ms, s, ns); 

    using(var wc = new WebClient()) { 
     wc.Encoding = System.Text.Encoding.UTF8; 
     var resp = wc.UploadData(
      "http://www.e-courier.com/ecourier/software/xml/XML.asp", 
      ms.ToArray() 
    ); 
    Console.WriteLine(Encoding.UTF8.GetString(resp)); 
    } 
} 

这里是DTO类以匹配XML负载的序列化结构:

[XmlRoot("Envelope", Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
public class Envelope { 
    [XmlElement("Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")] 
    public Body Body { get; set; } 
} 

public class Body { 
    [XmlElement("Login", Namespace="http://www.e-courier.com/software/schema/public/")] 
    public Login Login { get; set; } 
} 

public class Login { 
    [XmlAttribute("UserName")] 
    public string UserName { get; set; } 
    [XmlAttribute("Password")] 
    public string Password { get; set; } 
    [XmlAttribute("WebSite")] 
    public string WebSite { get; set; } 
} 
+0

感谢您的回复我有我想知道XSD的格式问题,例如中显示和您的答案不匹配。那么,为什么公司提供这样的例子,xsd的含义是什么,所以没有人能够理解他们将如何通过该值:) –

+0

XSD描述接口的合同。您的Visual Studio(和其他IDE)可以使用XSD并显示您的xml是否有效。 XSD在表达验证规则方面更加丰富,然后XMl片段可以表达一个示例。而且他们也提供了例子,所以我认为从一个转换到另一个并不难。无论如何,我已经添加了如何基于XSD来登录XElement。 – rene

+0

我有张贴xml和获得响应'<信封的xmlns = “http://schemas.xmlsoap.org/soap/envelope/”> \t \t \t <登录用户名= “测试” 密码= “测试” 的xmlns =“http://www.e-courier.com/software/schema/public/”/> \t –