c# - 读取 XSD 为 "anyType"且属性 xsi 为 :type ="string" 的 XML 元素

标签 c# xml saml xmlreader

我正在尝试读取一个 XML 文档(这是一个 SAML token ,但它不是特定的 SAML 相关问题)。 XSD(参见 here)在 XSD 的倒数第三行指定 元素,如下所示:

 <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>
  1. 哪个是有效的 XML? xsi:type="string"xsi:type="xs:string"

    /li>
  2. 我是否缺少 namespace 引用?

(这个XML消息是第三方生成的,所以我无法更改它,想知道它是否有效)。

更新

根元素确实包含以下 namespace 引用:

<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">
  1. 据我推测,默认命名空间是“http://thirdparty.co.uk/schema ”??

最佳答案

问题只是您正在使用的类型值的范围。根据您最近的更新,默认架构是 http://thirdparty.co.uk/schema

考虑场景:

 <saml:AttributeValue
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:type="string">
                Glasgow
 </saml:AttributeValue>

意味着解析器将尝试在默认架构中查找 string 类型,当然 string 未定义。

现在,当您将它更改为 xs:string 时,意味着您明确告诉您在命名空间别名 xs 中查找 string。这就是为什么下面是正确的:

 <saml:AttributeValue
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:type="xs:string">
                Glasgow
 </saml:AttributeValue>

虽然您收到的错误消息是一个验证错误,它可能适用于严格使用具有值的命名空间别名。

关于c# - 读取 XSD 为 "anyType"且属性 xsi 为 :type ="string" 的 XML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413429/

相关文章:

jquery - 使用 Javascript 或 JQuery 按元素值检索 XML 节点

.net - 具有 Angular 前端和 .NET 后端的 Azure SSO 的 SAML 配置

azure - SAML SSO 在 Websphere Liberty 中不起作用

c# - 在 WCF (C#) 中引用服务时出现问题

c# - 有没有办法查看 DynamicMethod 生成的 x86 汇编代码?

c# - 如何将委托(delegate)传递给另一个类

c# - 带有 System.Transactions.SafeIUnknown 的 .NET 垃圾收集器中的内存泄漏

mysql - MYSQL存储过程中的XML解析

c# - 首次创建序列化类时的默认条目

python - 在 Windows 中安装 Python-saml 包时出错