Blah Blah Blah Blah Blah Blah Blah Blah Blah

Extracting a simpletype enum from an attribute in an XSD

83 views Asked by At

Here is a snippet of the schema:

</xs:schema>
  <xs:attribute name="myFields">
        <xs:annotation>
          <xs:documentation>Blah Blah Blah</xs:documentation>
        </xs:annotation>
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="field_1"/>
            <xs:enumeration value="field_2"/>
            <xs:enumeration value="field_3_etc"/>
        </xs:restriction>
    </xs:simpleType>
  </xs:attribute>
</xs:schema>

How do I "get at" the simpleType? I want it as a list of strings ideally.

So far all I've managed to do is get hold of the attribute, because it has a name. I can't give the simpleType a name for some reason (Only top-level items are allowed a name, I think).

Here is what my code currently looks like:

        var schemaSet = new XmlSchemaSet();           
        schemaSet.Add("", XMLPath + SchemaFileName);
        schemaSet.Compile();


        var schema = schemaSet.Schemas().OfType<XmlSchema>().First();
        var attrs = schema.Items.OfType<XmlSchemaAttribute>()
            .First(x => x.Name == "myFields");
1

There are 1 answers

0
jimbo On

Okay, I figured it out.

I need to cast the AttributeSchemaType Contents as an XmlSchemaSimpleTypeRestriction and then get the "Facets" of that.

 var innerContentsOfRoot = (XmlSchemaSimpleTypeRestriction)attribute.AttributeSchemaType.Content;
 var strings = innerContentsOfRoot.Facets.OfType<XmlSchemaEnumerationFacet>().Select(d => d.Value);