使用" anyType"的XSD读取XML元素。和属性xsi:type =" string"

时间:2015-10-29 11:50:27

标签: c# xml saml xmlreader

我试图读取XML文档(这是一个SAML令牌,但它并不特别是与SAML相关的问题)。 XSD(请参阅here)指定 <在XSD的第3行最后一行上的saml:AttributeValue> 元素如下:

 <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 =&#34; string&#34; 的格式。如果我在 xsi的类型属性中包含 xs: :type =&#34; string&#34; (见下文),似乎工作正常。

      <saml:AttributeValue
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:type="xs:string">
                Glasgow
      </saml:AttributeValue>
  1. 哪个是有效的XML? xsi:type =&#34; string&#34; xsi:type =&#34; xs:string&#34;

  2. 我是否遗漏了命名空间参考?

  3. (此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">
    
    1. 我从中推测,默认命名空间是&#34; http://thirdparty.co.uk/schema&#34; ??

2 个答案:

答案 0 :(得分:3)

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.

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.

Now when you change it to xs:string means you're explicitly telling that look for string in namespace alias xs. 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.

答案 1 :(得分:0)

试试这个

<?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>​