c# - XmlWriter 不会正确生成我需要的命名空间

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

它要么没有显示,要么是错误的。

我无法弄清楚我缺少什么才能按照我需要的方式生成它。

最佳答案

以下代码可以运行并成功编写所需的 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)命名空间 作为第二个参数,但您传递的是命名空间 prefix "samlp"。所以那是行不通的。

  • 但是打电话

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

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

  • 显式编写带有指定命名空间和命名空间前缀的 AuthnRequest 元素后,实际上不再需要编写命名空间属性 -- XmlWriter将在属性列表的末尾自动为您完成。如果你想控制它在属性列表中的位置,你只需要手动编写命名空间属性。然而,根据XML Standard :

    Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.

    因此,您可以跳过它。

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

示例 fiddle 在这里:https://dotnetfiddle.net/99hu1I .

关于c# - XmlWriter 不会正确生成我需要的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51864429/

相关文章:

c# - XmlWriter 在 C# 中使用 StringWriter 编码 UTF-8

c# - C# 中 XMLwriter 的缩进和换行命令

c# - 在 EF 6 中模拟 DBContext 未按预期工作

c# - 如何查看是否安装了驱动程序?

c# - 如何为 Android 构建普通的 Mono?

c# - 将垂直滚动条添加到WPF网格

Android - 预览和设计 View 上的 "Exception raised during rendering: Could not initialize class libcore.util.ZoneInfoDB"

c# - .NET XmlSerializer(反)使用架构编译器 xsd.exe 和错误的命名空间绑定(bind)从不同的命名空间序列化派生类型

java - SAX 处理程序的 startElement 回调中的属性中不存在 xmlns 属性

java - 如何使用 JAXB 排除某些属性以将其转换为 xml