我想整合这个api但不了解如何以及在这个XML中为用户名和密码传递值。
<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。提前感谢您的时间。
答案 0 :(得分: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));
}
}
以下是与XML有效负载的序列化结构相匹配的DTO类:
[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; }
}