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:simpleTyp e name="color">
<xs:restricti on base="xs:string ">
<xs:enumerati on value="aqua"/>
<xs:enumerati on value="black"/>
<xs:enumerati on value="black"/>
<xs:enumerati on value="blue"/>
<xs:enumerati on value="fuchsia"/>
<xs:enumerati on value="gray"/>
<xs:enumerati on value="green"/>
<xs:enumerati on value="lime"/>
<xs:enumerati on value="maroon"/>
<xs:enumerati on value="navy"/>
<xs:enumerati on value="olive"/>
<xs:enumerati on value="purple"/>
<xs:enumerati on value="red"/>
<xs:enumerati on value="silver"/>
<xs:enumerati on value="teal"/>
<xs:enumerati on value="white"/>
<xs:enumerati on 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 DataGridViewCom boBoxColor to represent each.
Dim colColor As New DataGridViewCom boBoxColumn()
colColor.Header Text = "Color"
colColor.DataPr opertyName = kwords.keyword. colorColumn.Col umnName
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(N othing, My.Application. AssemblyInfo.Di rectoryPath +
"\keywords.xsd" )
Dim schema As XmlSchema
For Each schema In schemaset.Schem as()
Dim schobj As XmlSchemaObject
'iterate through types
For Each schobj In schema.SchemaTy pes.Values
If TypeOf (schobj) Is XmlSchemaSimple Type Then 'simple type
'cast to simple type
Dim simple As XmlSchemaSimple Type = _
CType(schobj, XmlSchemaSimple Type)
If TypeOf (simple.Content ) Is _
XmlSchemaSimple TypeRestriction Then 'has restriction
If (simple.Name = "color") Then 'color attribute
'cast Content to STR
Dim rest As XmlSchemaSimple TypeRestriction = _
CType(simple.Co ntent, _
XmlSchemaSimple TypeRestriction )
'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 XmlSchemaSimple TypeRestriction = _
CType(simple.Co ntent, _
XmlSchemaSimple TypeRestriction )
For Each facet As XmlSchemaFacet In rest.Facets
colWeight.Items .Add(facet.Valu e)
Next 'facet
End If 'Color
End If 'simle.Content
End If 'XmlSchemaSimpl eType
Next 'schobj
Next 'schema
Thanks!
---
http://code.box.sk
nemo me impune lacessit
*** Sent via Developersdex http://www.developersdex.com ***