Core Question: Is there a better way to dynamically pull the allowed
values of an attribute out of a dataset's schema.
I have an XML file that contains a string element with two attributes,
weight and color. for example,
<keyword color="blue" weight="normal">And</keyword>
the color and weight attributes are restricted to only certain values
(color to the 16 named colors in HTML; weight to normal, bold or italic
-- the defaults being blue and normal, respectively). for example,
<xs:simpleType name="color">
<xs:restriction base="xs:string">
<xs:enumeration value="aqua"/>
<xs:enumeration value="black"/>
<xs:enumeration value="black"/>
<xs:enumeration value="blue"/>
<xs:enumeration value="fuchsia"/>
<xs:enumeration value="gray"/>
<xs:enumeration value="green"/>
<xs:enumeration value="lime"/>
<xs:enumeration value="maroon"/>
<xs:enumeration value="navy"/>
<xs:enumeration value="olive"/>
<xs:enumeration value="purple"/>
<xs:enumeration value="red"/>
<xs:enumeration value="silver"/>
<xs:enumeration value="teal"/>
<xs:enumeration value="white"/>
<xs:enumeration value="yellow"/>
</xs:restriction>
</xs:simpleType>
I also have a VS-generated typed DataSet which I use to populate a
DataGridView. since I want to limit the values of these two attributes,
I am using a DataGridViewComboBoxColor to represent each.
Dim colColor As New DataGridViewComboBoxColumn()
colColor.HeaderText = "Color"
colColor.DataPropertyName = kwords.keyword.colorColumn.ColumnName
two parts are giving me fits
1) getting the allowed values of each column from the DataSet -- I see
no easy way to extract the schema from the DataSet in memory and have
resorted to reading the schema directly from the file an additional time
-- IS THERE A WAY TO DIRECTLY PULL THE SCHEMA OUT OF A DataSet?
2) the procedure for pulling the allowed values from the enumeration
seems overly complex -- IS THERE A BETTER WAY THAN THIS?
here's working code:
'TODO: this seems awkward -- find a better way
Dim schemaset As New XmlSchemaSet()
schemaset.Add(Nothing, My.Application.AssemblyInfo.DirectoryPath +
"\keywords.xsd")
Dim schema As XmlSchema
For Each schema In schemaset.Schemas()
Dim schobj As XmlSchemaObject
'iterate through types
For Each schobj In schema.SchemaTypes.Values
If TypeOf (schobj) Is XmlSchemaSimpleType Then 'simple type
'cast to simple type
Dim simple As XmlSchemaSimpleType = _
CType(schobj, XmlSchemaSimpleType)
If TypeOf (simple.Content) Is _
XmlSchemaSimpleTypeRestriction Then 'has restriction
If (simple.Name = "color") Then 'color attribute
'cast Content to STR
Dim rest As XmlSchemaSimpleTypeRestriction = _
CType(simple.Content, _
XmlSchemaSimpleTypeRestriction)
'each enum value shows as a facet
For Each facet As XmlSchemaFacet In rest.Facets
'add value to combo box items
colColor.Items.Add(facet.Value)
Next 'facet
ElseIf (simple.Name = "weight") Then 'weight attribute
Dim rest As XmlSchemaSimpleTypeRestriction = _
CType(simple.Content, _
XmlSchemaSimpleTypeRestriction)
For Each facet As XmlSchemaFacet In rest.Facets
colWeight.Items.Add(facet.Value)
Next 'facet
End If 'Color
End If 'simle.Content
End If 'XmlSchemaSimpleType
Next 'schobj
Next 'schema
Thanks!
---
http://code.box.sk
nemo me impune lacessit
*** Sent via Developersdex http://www.developersdex.com ***