【发布时间】:2015-10-29 11:50:27
【问题描述】:
我正在尝试读取一个 XML 文档(它是一个 SAML 令牌,但它不是专门与 SAML 相关的问题)。 XSD(请参阅here)在 XSD 的最后 3 行指定
<element name="AttributeValue" type="anyType" nillable="true"/>
它说它可以是 anyType,这很好。
但实际的 XML 已尝试指定命名空间和类型,而我的 XMLReader 在格式不正确之前正在努力读取它。
<saml:AttributeStatement>
<saml:Attribute Name="Town">
<saml:AttributeValue
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="string">
Glasgow
</saml:AttributeValue>
</saml:Attribute>
</saml:AttributeStatement>
当我尝试使用此代码读取 XML 时...
//This code just simulates the XmlReader navigating over the document, rather than a specific function.
var sr = new StringReader(xmlString);
var reader = XmlReader.Create(sr);
var output = reader.ReadSubTree();
我收到此错误...
{"ID4254: The AttributeValueXsiType of a SAML Attribute must be a string of the form 'prefix#suffix', where prefix and suffix are non-empty strings.\r\nParameter name: value"}
这是System.IdentityModel.Tokens.SamlAttribute类抛出的,见代码here
我认为是 xsi:type="string" 的格式导致了问题。如果我在 xsi:type="string" 的类型属性中包含 xs:(见下文),它似乎工作正常。
<saml:AttributeValue
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:string">
Glasgow
</saml:AttributeValue>
-
哪个是有效的 XML? xsi:type="string" 或 xsi:type="xs:string"
我是否缺少命名空间引用?
(这个XML消息是由第三方生成的,所以我无法更改它,想知道它是否有效)。
更新
根元素确实包含以下命名空间引用:
<Request xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol"
xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion"
xmlns="http://thirdparty.co.uk/schema">
- 据此推测,默认命名空间是“http://thirdparty.co.uk/schema”??
【问题讨论】:
-
能否请您发布引发错误的实际代码?目前它无法通过模拟阅读器重现