简体   繁体   English

读取XSD为“anyType”且属性为xsi的XML元素:type =“string”

[英]Read XML element with XSD of “anyType” and attribute xsi:type=“string”

I'm trying to read an XML document (which is a SAML token, but its not specifically a SAML related problem). 我正在尝试读取XML文档(这是一个SAML令牌,但它不是特定的SAML相关问题)。 The XSD (see here ) specifies the < saml:AttributeValue> element on the 3rd last line of the XSD like this: XSD(参见此处 )指定XSD第3行的<saml:AttributeValue>元素,如下所示:

 <element name="AttributeValue" type="anyType" nillable="true"/>

It says it can be anyType which is fine. 它说它可以是anyType ,这很好。

But the actual XML has tried to specify the namespace and type and my XMLReader is struggling to read it before its not formatted correctly. 但实际的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>

When I try to read the XML using this code... 当我尝试使用此代码读取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();

I get this error... 我收到这个错误......

{"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"}

This is thrown by the System.IdentityModel.Tokens.SamlAttribute class, see the code here 这是由System.IdentityModel.Tokens.SamlAttribute类引发的,请参阅此处的代码

I believe it is the format of the xsi:type="string" which is causing a problem. 我相信这是xsi:type =“string”的格式,这会导致问题。 If I include xs: in the type attribute of xsi:type="string" (see below), it seems to work fine. 如果我在xsi:type =“string”的type属性中包含xs :(见下文),它似乎工作正常。

      <saml:AttributeValue
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:type="xs:string">
                Glasgow
      </saml:AttributeValue>
  1. Which is valid XML? 哪个是有效的XML? xsi:type="string" or xsi:type="xs:string" xsi:type =“string”xsi:type =“xs:string”

  2. Am I missing a namespace reference? 我错过了命名空间参考吗?

(This XML message is generated by a third party, so I can't change it and want to know if its valid or not). (此XML消息由第三方生成,因此我无法更改它并想知道它是否有效)。

Update 更新

The root element does contain the following namespace references: 根元素包含以下命名空间引用:

<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. I presume from this, the default namespace is " http://thirdparty.co.uk/schema "?? 我从这个假设,默认名称空间是“ http://thirdparty.co.uk/schema ”??

The problem is simply with scope of the type value you're using. 问题仅在于您正在使用的类型值的范围。 As per your last update in question the default schema is http://thirdparty.co.uk/schema . 根据您上次的更新,默认架构是http://thirdparty.co.uk/schema

Considering scenario: 考虑场景:

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

means the parser will try to find the string type in default schema where ofcourse string is not defined. 表示解析器将尝试在默认架构中找到string类型,其中未定义ofcourse string

Now when you change it to xs:string means you're explicitly telling that look for string in namespace alias xs . 现在,当您将其更改为xs:string意味着您明确告诉在命名空间别名xs中查找string That's why below is the correct one: 这就是为什么下面是正确的:

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

Although the error message you're getting is a validation error which might be applied to strict the usage of namespace alias with value. 虽然您收到的错误消息是一个验证错误,可能适用于严格使用带有值的命名空间别名。

Try this 尝试这个

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns:saml="anything">
<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>
</Root>​

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM