2017-07-28 210 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 –