Question : 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 Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
'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



  
    
      
        
        
        
      
    
  
  
    
      
        
          
            
              
              
              
              
              
              
              
              
              
              
              
              
            
          
        
      
    
  

Answer : How to read field names from an XSD file.

Oh, I get it. Here is the problem. The link has no knowledge about the instance of your class after it is rendered, therefore the way you think to add a listener is simply impossible. To do what you want you need to have a reference to the link element and add a listener to it inside the class itself. I've attached a quick and dirty example. It will not work in IE though because M$ have their own way of adding listeners via attachEvent() method. Google it. I wouldn't also use document.write() to write html. I'd rather create the elements with document.createElement() which also returns a reference to the elementso you can add listeners to it, and then add the created elements to other existing DOM elements. Hope you get the idea.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
function testClass(){
    this.display = function(){
        document.write('<input type="checkbox" name="test" id="test" />');
        document.write('<a id="link" href="#">Check Box</a>');
        var link = document.getElementById('link');
        link.addEventListener('click', this.checkBox, false);
    }
    
    this.checkBox = function(e) {
        var checkbox = document.getElementById('test');
        checkbox.checked = !checkbox.checked;
        e.stopPropagation();
        e.preventDefault();
    }
}
Random Solutions  
 
programming4us programming4us