类序列化和命名空间(xmlns)

时间:2013-09-09 16:00:38

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

我有一个我正在序列化的课程

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">

1 个答案:

答案 0 :(得分:0)

OnWriteHeaderContents 方法

中尝试此选项
writer.WriteStartElement("saml", "Assertion", "urn:oasis:names:tc:SAML:2.0:assertion");
writer.WriteString("Value");
writer.WriteEndElement();