How to read field names from an XSD file.

Posted on 2009-12-30
Last Modified: 2012-05-08
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!


'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)

            '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


            Dim compiledSchema As XmlSchema = Nothing

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

            schema = compiledSchema

            If schema.IsCompiled Then
            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)
                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="" xmlns:mstns="" xmlns="" xmlns:xs="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:msprop="urn:schemas-microsoft-com:xml-msprop" attributeFormDefault="qualified" elementFormDefault="qualified">
    <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 />
  <xs:element name="dsDataArchive" msdata:IsDataSet="true" msdata:UseCurrentLocale="true" msprop:Generator_DataSetName="dsDataArchive" msprop:Generator_UserDSName="dsDataArchive" msprop:EnableTableAdapterManager="true">
      <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: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" />

Open in new window

Question by:sharizod
    LVL 3

    Accepted Solution

    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.

    Have a great day!
    LVL 1

    Author Comment

    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
            gvSortFields.DataSource = ds.Tables(0)
            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"
                    xsdFile = ".\Reports\DATA\ds" & cboReportList.Text & ".xsd"
                End If
                myStreamReader = New StreamReader(MapPath(xsdFile))
                ds = xmlDoc.DataSet
                With xmlDoc.DataSet.Tables(0)
                    For n = 0 To .Columns.Count - 1
                        AddRowsToSortGrid(dt, .Columns(n).ColumnName.ToString)
                End With
                gvSortFields.DataSource = dt
                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
                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
                Dim dr As DataRow = dt.NewRow
                dr.Item("FIeldName") = sFieldName
            End If
            Return dt
        End Function

    Open in new window


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now