我正在尝试使用类/ XML装饰器实现以下输出:
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Timestamp xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<Created>2019-05-28T15:10:45Z</Created>
<Expires>2019-05-28T15:15:45Z</Expires>
</Timestamp>
<UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<Username>*****</Username>
<Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*****</Password>
</UsernameToken>
</wsse:Security>
这里的痛点是在Timestamp / Security元素上具有'xmlns'属性,在UsernameToken元素上具有'xmlns:wsu'属性,也没有WSU前缀。
这些是我各自的课程:
[XmlRoot(ElementName = "Security", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class Security
{
[XmlElement(ElementName = "Timestamp", Namespace = "")]
public Timestamp Timestamp { get; set; }
[XmlElement(ElementName = "UsernameToken")]
public UsernameToken UsernameToken { get; set; }
[XmlAttribute(AttributeName = "wsse", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Wsse { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
[XmlRoot(ElementName = "Timestamp", Namespace = "")]
public class Timestamp
{
[XmlElement(ElementName = "Created", Namespace = "")]
public string Created { get; set; }
[XmlElement(ElementName = "Expires", Namespace = "")]
public string Expires { get; set; }
}
[XmlRoot(ElementName = "UsernameToken", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public class UsernameToken
{
[XmlElement(ElementName = "Username", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public string Username { get; set; }
[XmlElement(ElementName = "Password", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")]
public Password Password { get; set; }
[XmlAttribute(AttributeName = "Id", Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")]
public string Id { get; set; }
[XmlAttribute(AttributeName = "wsu", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Wsu { get; set; }
}
将它们添加到我的名称空间序列化器中:
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
ns.Add("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
我得到以下输出:
<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<Timestamp>
<Created>2019-05-28T16:13:18Z</Created>
<Expires>2019-05-28T16:18:18Z</Expires>
</Timestamp>
<wsse:UsernameToken wsu:Id="UsernameToken-1" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>*****</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">*****</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
我正在使用Namespace =“”(例如在Timestamp类上)作为不包含前缀的方式。我觉得这是不正确的用法。似乎也从元素中删除了xmlns属性。
赞赏可以提供任何信息以实现所需的输出!谢谢