C# XmlWriter赢得';无法正确生成所需的名称空间
我正在尝试生成与此格式匹配的XML数据:C# XmlWriter赢得';无法正确生成所需的名称空间,c#,xml,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:S
<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>
以下是我的一些尝试和结果
尝试#1
结果#1
与上述消息一起崩溃。
尝试#2
结果#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();
}
注:
- 该方法将名称空间作为第二个参数,但您传递的是名称空间前缀
。所以那没用“samlp”
- 但是打电话
也会失败,因为这样做会将“urn:oasis:names:tc:SAML:2.0:protocol”建立为默认名称空间,而您希望它是前缀为xw.WriteStartElement("AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
的非默认名称空间。因此必须使用sampl
- 使用指定的名称空间和名称空间前缀显式地编写了
元素后,实际上不再需要再编写名称空间属性--AuthnRequest
将在属性列表的末尾自动为您编写。如果要控制名称空间属性在属性列表中的位置,只需手动编写名称空间属性。然而,根据该报告: 请注意,属性规范在开始标记或空元素标记中的顺序并不重要 所以,你可以跳过这个XmlWriter
- 类中的方法可用于将非字符串原语从XML正确转换为XML
这里的fiddle示例:。我发现,当您有复杂的名称空间时,只解析字符串要容易得多。使用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);
stringxml=
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
<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"
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();
}
xw.WriteStartElement("AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
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);