XmlWriter不会生成我正确需要的名称空间

时间:2018-08-15 18:29:10

标签: c# xml xmlwriter

我正在尝试生成与此格式匹配的XML数据:

<samlp:AuthnRequest
        xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
        xmlns="urn:oasis:names:tc:SAML:2.0:assertion"
        IssueInstant="2018-07-04T19:19:53.284Z"
        ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
        Version="2.0">
    <samlp:NameIDPolicy
            AllowCreate="true"
            Format="urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified"/>
</samlp:AuthnRequest>

由于匿名原因,它显然缺少很多标签。

我正在使用XmlWriter

我设法使用不同的WriteStartElement()和WriteAttributeString()集来生成此数据,但是我永远无法正确处理它。结果要么略有不同,要么只是像XmlException一样崩溃一半

System.Xml.XmlException: 'The prefix '' cannot be redefined from 'samlp' to
'urn:oasis:names:tc:SAML:2.0:assertion' within the same start element tag.'

这是我的一些尝试和结果。

尝试#1

using (XmlWriter xw = XmlWriter.Create(sw, xws))
{
    xw.WriteStartElement("AuthnRequest", "samlp");
    xw.WriteAttributeString("xmlns", "samlp", null, "urn:oasis:names:tc:SAML:2.0:protocol");
    xw.WriteAttributeString("xmlns", "urn:oasis:names:tc:SAML:2.0:assertion");
    // ...
}

结果#1

Crashed with the above message.

尝试#2

using (XmlWriter xw = XmlWriter.Create(sw, xws))
{
    xw.WriteStartElement("AuthnRequest", "samlp");
    xw.WriteAttributeString("xmlns", "samlp", null, "urn:oasis:names:tc:SAML:2.0:protocol");
    // ...
}

结果#2

<AuthnRequest
    xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
    ForceAuthn="false" ID="ID_4f85b6d1-a839-4899-972c-12275bf8711c"
    IssueInstant="2018-08-15T18:23:49Z"
    ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST"
    Version="2.0"
    xmlns="samlp">
    ...

其余的尝试都是这两种方法的细微变化,但都围绕参数移动,但没有一个与我期望的结果相近。

具体来说,我在生成此属性时遇到很多麻烦:

xmlns="urn:oasis:names:tc:SAML:2.0:assertion"

它没有显示出来或者是错误的。

我无法弄清楚要以我需要的方式生成它所缺少的内容。

2 个答案:

答案 0 :(得分:2)

以下代码可以正常工作并成功编写所需的XML:

var issueInstant = DateTime.Parse("2018-07-04T19:19:53.284Z", CultureInfo.InvariantCulture);

using (var xw = XmlWriter.Create(sw, xws))
{
    var samplNs = "urn:oasis:names:tc:SAML:2.0:protocol";
    var defaultNs = "urn:oasis:names:tc:SAML:2.0:assertion";

    // XmlWriter.WriteStartElement(String prefix, String localName, String ns)
    xw.WriteStartElement("samlp", "AuthnRequest", samplNs);

    // Write the xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" namespace.
    // Actually this is redundant as the attribute will be automatically be written at the end of the
    // attribute list do to the previous call to xw.WriteStartElement().  Call it here only if, for some
    // reason, you need to control the attribute order.
    xw.WriteAttributeString("xmlns", "samlp", null, samplNs);               

    // Write the default namespace
    xw.WriteAttributeString("xmlns", defaultNs);

    // Write attribute values.              
    xw.WriteAttributeString("IssueInstant", XmlConvert.ToString(issueInstant, XmlDateTimeSerializationMode.Utc));
    xw.WriteAttributeString("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    xw.WriteAttributeString("Version", "2.0");

    // Write samlp:NameIDPolicy
    // No need to specify prefix since it is specified in the document root.
    xw.WriteStartElement("NameIDPolicy", samplNs);
    xw.WriteAttributeString("AllowCreate", XmlConvert.ToString(true));
    xw.WriteAttributeString("Format", "urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified");

    // Write the end of NameIDPolicy
    xw.WriteEndElement();

    // Write the end of AuthnRequest
    xw.WriteEndElement();
}       

注意:

  • 方法XmlWriter.WriteStartElement(String localName, String ns)命名空间作为第二个参数,但是您正在传递命名空间前缀"samlp" 。所以那行不通。

  • 但是打电话给

    xw.WriteStartElement("AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol"); 
    

    也会失败,因为这样做会将"urn:oasis:names:tc:SAML:2.0:protocol"建立为 default 命名空间,而您希望它成为具有前缀sampl的非默认命名空间。因此必须使用XmlWriter.WriteStartElement(String prefix, String localName, String ns)

  • 已经使用指定的名称空间和名称空间前缀明确编写了AuthnRequest元素,实际上不再需要再编写名称空间属性-XmlWriter会自动为您完成此操作在属性列表的末尾。如果要控制名称空间属性在属性列表中的位置,则只需手动编写它。但是,根据XML Standard

      

    请注意,开始标签或空元素标签中的属性规范顺序并不重要。

    所以,您可以跳过它。

  • 来自XmlConvert类的方法可用于正确地将非字符串基元与XML相互转换。

这里的小提琴样本:https://dotnetfiddle.net/99hu1I

答案 1 :(得分:1)

我发现,当您使用复杂的名称空间时,解析字符串要容易得多。使用xml linq:

            string xml =
                "<samlp:AuthnRequest" +
                    " xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"" +
                    " xmlns=\"urn:oasis:names:tc:SAML:2.0:assertion\"" +
                    " IssueInstant=\"2018-07-04T19:19:53.284Z\"" +
                    " ProtocolBinding=\"urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST\"" +
                    " Version=\"2.0\">" +
                    "<samlp:NameIDPolicy" +
                       " AllowCreate=\"true\"" +
                       " Format=\"urn:oasis:names:tc:SAML:1.1:nameid-format:unspecified\"/>" +
                "</samlp:AuthnRequest>";

            XDocument doc = XDocument.Parse(xml);