【问题标题】:Class serialization and namespace (xmlns)类序列化和命名空间 (xmlns)
【发布时间】:2013-09-09 16:00:37
【问题描述】:

我有一个要序列化的类

public partial class Security : MessageHeader
{
    private Assertion assertionField;

    [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:oasis:names:tc:SAML:2.0:assertion")]
    public Assertion Assertion
    {
        get
        {
            return this.assertionField;
        }
        set
        {
            this.assertionField = value;
        }
    }

    public override string Name
    {
        get { return "Security"; }
    }

    public override string Namespace
    {
        get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; }
    }

    [XmlIgnoreAttribute]
    public string UserID { get; set; }

    [XmlIgnoreAttribute]
    public string FirstName { get; set; }

    [XmlIgnoreAttribute]
    public string LastName { get; set; }

    [XmlIgnoreAttribute]
    public string ReasonForSearch { get; set; }

    public Security() 
    {
        Assertion = new Assertion(UserID, FirstName, LastName, ReasonForSearch);
    }

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
        XmlSerializer serializer = new XmlSerializer(typeof(Assertion));
        serializer.Serialize(writer, Assertion, ns);
    }
}

这就是我添加代码头的方式

using (OperationContextScope scope = new OperationContextScope(healthixClient.InnerChannel))
        {
            Security msgHdr = new Security();
            msgHdr.UserID = "TestUserID";
            msgHdr.FirstName = "TestUserFirstName";
            msgHdr.LastName = "TestUserLastName";
            msgHdr.ReasonForSearch = "ReasonForSearch";

            OperationContext.Current.OutgoingMessageHeaders.Add(msgHdr);
        }

当我将它序列化并添加到我的代码标题中时,它看起来像这样

 <Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-09-09T15:38:16Z" Version="2.0" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"> 
 < rest of the Xml is correct >

现在,如果我只将重写 OnWriteHeaderContents 方法更改为

protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
    {
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
        XmlSerializer serializer = new XmlSerializer(typeof(Security));
        serializer.Serialize(writer, new Security(), ns);
    }

标题看起来像这样

<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
    <Security xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
        <saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-09-09T15:29:09Z" Version="2.0">
        < rest of the Xml is correct >

我希望标题看起来像这样

<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion">
    <saml:Assertion ID="saml_6691a2b1-2a08-4d10-9d90-b006727d0e02" IssueInstant="2013-07-29T20:17:30.846Z" Version="2.0" xmlns="urn:oasis:names:tc:SAML:2.0:assertion">

【问题讨论】:

  • 我想在同一个元素stackoverflow.com/a/945246/1236044 上声明多个命名空间应该很有趣(也许 OnWriteStartHeader 也是一个更好的位置)
  • 这没有帮助。我的问题是将我的序列化对象添加到消息头。
  • 请向我们展示您说“其余都是正确的”的元素之一。这将告诉我们您所说的“正确”是什么意思。

标签: wcf c#-4.0 soap xml-serialization saml-2.0


【解决方案1】:

OnWriteHeaderContents方法中试试这个选项

writer.WriteStartElement("saml", "Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.WriteString("Value");
writer.WriteEndElement();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-08
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-01
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多