How to read field names from an XSD file.

Hi Experts,

I have the code snippet I am using that I snatched from the MSDN site and it does seem to iterate through the XSD file that I
am trying to get the field names of all the fields that exist in the XSD file.  I am trying to parse that fields so that I can set
a value in a column on a gridview with the field name from the XSD file.  I tried to access finer details in each XMLSchemaObject,
but it remains hidden or otherwise unavailable to me in the example given below.  

Any help would be most welcome!

TIA,
Bob

'Code to walk the XSD File
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Xml.Schema
Imports System.Xml
Imports System.Reflection

Public Class cValidXSD

    Public Shared Sub Main(ByVal FilePath As String)

        'Dim xsd As String = "example.xsd"

        Dim schema As XmlSchema

        Dim reader As XmlTextReader = New XmlTextReader(FilePath)



        Try
            'fs = New FileStream(xsd, FileMode.Open)
            'schema = XmlSchema.Read(fs, New ValidationEventHandler(AddressOf ShowCompileError))

            schema = XmlSchema.Read(reader, Nothing)

            Dim schemaSet As New XmlSchemaSet()
            AddHandler schemaSet.ValidationEventHandler, AddressOf ShowCompileError

            schemaSet.Add(schema)
            schemaSet.Compile()

            Dim compiledSchema As XmlSchema = Nothing

            For Each schema1 As XmlSchema In schemaSet.Schemas()
                compiledSchema = schema1
            Next

            schema = compiledSchema

            If schema.IsCompiled Then
                DisplayObjects(schema)
            End If

        Catch e As XmlSchemaException
            Console.WriteLine("LineNumber = {0}", e.LineNumber)
            Console.WriteLine("LinePosition = {0}", e.LinePosition)
            Console.WriteLine("Message = {0}", e.Message)
            Console.WriteLine("Source = {0}", e.Source)

        End Try
    End Sub 'Main


    Private Overloads Shared Sub DisplayObjects(ByVal o As Object)
        DisplayObjects(o, "")
    End Sub 'DisplayObjects

    Private Overloads Shared Sub DisplayObjects(ByVal o As Object, ByVal indent As String)
        Console.WriteLine("{0}{1}", indent, o)

        Dim property1 As PropertyInfo
        For Each property1 In o.GetType().GetProperties()
            If property1.PropertyType.FullName = "System.Xml.Schema.XmlSchemaObjectCollection" Then

                Dim childObjectCollection As XmlSchemaObjectCollection = CType(property1.GetValue(o, Nothing), XmlSchemaObjectCollection)

                Dim schemaObject As XmlSchemaObject
                For Each schemaObject In childObjectCollection
                    DisplayObjects(schemaObject, indent + ControlChars.Tab)
                    'debug.writeline(schemaobject.xmlschemaappinfo.
                Next schemaObject
            End If
        Next property1
    End Sub 'DisplayObjects

    Private Shared Sub ShowCompileError(ByVal sender As Object, ByVal e As ValidationEventArgs)
        Console.WriteLine("Validation Error: {0}", e.Message)
    End Sub 'ShowCompileError
End Class 'ValidXSD



'XSD File I am trying to read

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="dsDataArchive" targetNamespace="http://tempuri.org/dsDataArchive.xsd" xmlns:mstns="http://tempuri.org/dsDataArchive.xsd" xmlns="http://tempuri.org/dsDataArchive.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:annotation>
    <xs:appinfo source="urn:schemas-microsoft-com:xml-msdatasource">
      <DataSource DefaultConnectionIndex="0" FunctionsComponentName="QueriesTableAdapter" Modifier="AutoLayout, AnsiClass, Class, Public" SchemaSerializationMode="IncludeSchema" xmlns="urn:schemas-microsoft-com:xml-msdatasource">
        <Connections />
        <Tables />
        <Sources />
      </DataSource>
    </xs:appinfo>
  </xs:annotation>
  <xs:element name="dsDataArchive" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="dsDataArchive" msprop:Generator_UserDSName="dsDataArchive" msprop:EnableTableAdapterManager="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="dtDataArchive" msprop:Generator_UserTableName="dtDataArchive" msprop:Generator_RowDeletedName="dtDataArchiveRowDeleted" msprop:Generator_TableClassName="dtDataArchiveDataTable" msprop:Generator_RowChangedName="dtDataArchiveRowChanged" msprop:Generator_RowClassName="dtDataArchiveRow" msprop:Generator_RowChangingName="dtDataArchiveRowChanging" msprop:Generator_RowEvArgName="dtDataArchiveRowChangeEvent" msprop:Generator_RowEvHandlerName="dtDataArchiveRowChangeEventHandler" msprop:Generator_TablePropName="dtDataArchive" msprop:Generator_TableVarName="tabledtDataArchive" msprop:Generator_RowDeletingName="dtDataArchiveRowDeleting">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="UnitName" msprop:Generator_UserColumnName="UnitName" msprop:Generator_ColumnPropNameInRow="UnitName" msprop:Generator_ColumnVarNameInTable="columnUnitName" msprop:Generator_ColumnPropNameInTable="UnitNameColumn" type="xs:string" minOccurs="0" />
              <xs:element name="Operator" msprop:Generator_UserColumnName="Operator" msprop:Generator_ColumnPropNameInRow="_Operator" msprop:Generator_ColumnVarNameInTable="columnOperator" msprop:Generator_ColumnPropNameInTable="OperatorColumn" type="xs:string" minOccurs="0" />
              <xs:element name="System" msprop:Generator_UserColumnName="System" msprop:Generator_ColumnPropNameInRow="System" msprop:Generator_ColumnVarNameInTable="columnSystem" msprop:Generator_ColumnPropNameInTable="SystemColumn" type="xs:string" minOccurs="0" />
              <xs:element name="_x005B_DateTime_x005D_" msprop:Generator_UserColumnName="[DateTime]" msprop:Generator_ColumnPropNameInRow="[DateTime]" msprop:Generator_ColumnVarNameInTable="_column_DateTime_" msprop:Generator_ColumnPropNameInTable="__DateTime_Column" type="xs:string" minOccurs="0" />
              <xs:element name="Type" msprop:Generator_UserColumnName="Type" msprop:Generator_ColumnPropNameInRow="Type" msprop:Generator_ColumnVarNameInTable="columnType" msprop:Generator_ColumnPropNameInTable="TypeColumn" type="xs:string" minOccurs="0" />
              <xs:element name="Message" msprop:Generator_UserColumnName="Message" msprop:Generator_ColumnPropNameInRow="Message" msprop:Generator_ColumnVarNameInTable="columnMessage" msprop:Generator_ColumnPropNameInTable="MessageColumn" type="xs:string" minOccurs="0" />
              <xs:element name="RawOdo" msprop:Generator_UserColumnName="RawOdo" msprop:Generator_ColumnPropNameInRow="RawOdo" msprop:Generator_ColumnVarNameInTable="columnRawOdo" msprop:Generator_ColumnPropNameInTable="RawOdoColumn" type="xs:string" minOccurs="0" />
              <xs:element name="RollingOdo" msprop:Generator_UserColumnName="RollingOdo" msprop:Generator_ColumnPropNameInRow="RollingOdo" msprop:Generator_ColumnVarNameInTable="columnRollingOdo" msprop:Generator_ColumnPropNameInTable="RollingOdoColumn" type="xs:string" minOccurs="0" />
              <xs:element name="TotalOdo" msprop:Generator_UserColumnName="TotalOdo" msprop:Generator_ColumnPropNameInRow="TotalOdo" msprop:Generator_ColumnVarNameInTable="columnTotalOdo" msprop:Generator_ColumnPropNameInTable="TotalOdoColumn" type="xs:string" minOccurs="0" />
              <xs:element name="RouteName" msprop:Generator_UserColumnName="RouteName" msprop:Generator_ColumnPropNameInRow="RouteName" msprop:Generator_ColumnVarNameInTable="columnRouteName" msprop:Generator_ColumnPropNameInTable="RouteNameColumn" type="xs:string" minOccurs="0" />
              <xs:element name="FromDate" msprop:Generator_UserColumnName="FromDate" msprop:Generator_ColumnPropNameInRow="FromDate" msprop:Generator_ColumnVarNameInTable="columnFromDate" msprop:Generator_ColumnPropNameInTable="FromDateColumn" type="xs:string" minOccurs="0" />
              <xs:element name="ToDate" msprop:Generator_UserColumnName="ToDate" msprop:Generator_ColumnPropNameInRow="ToDate" msprop:Generator_ColumnVarNameInTable="columnToDate" msprop:Generator_ColumnPropNameInTable="ToDateColumn" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

Open in new window

LVL 1
sharizodAsked:
Who is Participating?
 
tsqlguyConnect With a Mentor Commented:
This article should get you started if you have any other questions feel free to ask and i'll see what I can do. This is where I grabbed most of my information from for the same attempts.

http://quickstart.developerfusion.co.uk/quickstart/howto/doc/Xml/DataSetMapXSDSchema.aspx

Have a great day!
0
 
sharizodAuthor Commented:
Thanks tSQLGuy!  The link you gave helped me out.  I had to do a bit more on my end but.

The code follows:


Dim xsdFile As String = ""

        Dim schema As XmlSchema
        Dim myStreamReader As StreamReader = Nothing
        Dim xmlDoc As New XmlDataDocument


        Dim ds As New DataSet
        Dim dt As New DataTable


        ds.Tables.Add(dt)

        gvSortFields.DataSource = ds.Tables(0)
        gvSortFields.DataBind()

        dt.Columns.Add("FieldName", System.Type.GetType("System.String"))
        dt.Columns.Add("ShowInRpt", System.Type.GetType("System.Boolean"))
        dt.Columns.Add("Order", System.Type.GetType("System.Int32"))
        dt.Columns.Add("Desc Order", System.Type.GetType("System.Boolean"))


        If cboReportList.Text <> "" Then

            If Session("ReportType") = "GPS" Then
                xsdFile = ".\Reports\GPS\ds" & cboReportList.Text & ".xsd"
            Else
                xsdFile = ".\Reports\DATA\ds" & cboReportList.Text & ".xsd"
            End If


            myStreamReader = New StreamReader(MapPath(xsdFile))
            xmlDoc.DataSet.ReadXmlSchema(myStreamReader)
            ds = xmlDoc.DataSet

            With xmlDoc.DataSet.Tables(0)
                For n = 0 To .Columns.Count - 1
                    AddRowsToSortGrid(dt, .Columns(n).ColumnName.ToString)
                Next
            End With

            gvSortFields.DataSource = dt
            gvSortFields.DataBind()

            lblFromDate.Enabled = True
            lblToDate.Enabled = True
            lblSortColumns.Enabled = True
            lblSpeedUnits.Enabled = True
            GMDPFromDate.Enabled = True
            GMDPToDate.Enabled = True
            rbtnlstSpeedUnits.Enabled = True
            gvSortFields.Enabled = True
            btnRender.Enabled = True
            btnRenderToMap.Enabled = True

        Else

            lblFromDate.Enabled = False
            lblToDate.Enabled = False
            lblSortColumns.Enabled = False
            lblSpeedUnits.Enabled = False
            GMDPFromDate.Enabled = False
            GMDPToDate.Enabled = False
            rbtnlstSpeedUnits.Enabled = False
            gvSortFields.Enabled = False
            btnRender.Enabled = False
            btnRenderToMap.Enabled = False

        End If

    Private Function AddRowsToSortGrid(ByVal dt As DataTable, ByVal sFieldName As String) As DataTable

        If UCase(sFieldName) = "FROMDATE" Or UCase(sFieldName) = "TODATE" Then
        Else

            Dim dr As DataRow = dt.NewRow

            dr.Item("FIeldName") = sFieldName
            dt.Rows.Add(dr)

        End If

        Return dt

    End Function

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.