Learn how to a build a cloud-first strategyRegister Now


How to read field names from an XSD file.

Posted on 2009-12-30
Medium Priority
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="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: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

Accepted Solution

tsqlguy earned 2000 total points
ID: 26148827
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!

Author Comment

ID: 26155391
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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