Generating XML Schema Dynamically Using VB.NET 2005: Essentials - Understanding the dynamic generation of XML Schema using the .NET framework
(Page 2 of 4 )
This section explains the code fragment listed in the previous section. Let us examine it part by part. First consider the following:
Imports System
Imports System.Xml
Imports System.Xml.Schema
The above makes class libraries (related to XML) available for our application. We shall clear the textbox information using the following statement:
Me.TextBox1.Text = ""
Proceeding further, we have the following statement:
Dim schema As New XmlSchema()
The above statement really starts our XML Schema. It creates an object (“schema”) of type “XmlSchema.” The class “XmlSchema” basically exists in the namespace “System.Xml.Schema,” which resides in “System.XML.dll” assembly. This would indirectly add the top most processing instruction, which defines the XML document version, and so on.
Once a schema object is created, we can start adding our own elements, attributes, processing instructions, comments, and so on to the same schema object. Further proceeding, we have the following:
Dim elementOrg As New XmlSchemaElement()
The above statement creates a simple XML Schema element (but no descriptions of the element yet!). Further proceeding we have:
schema.Items.Add(elementOrg)
elementOrg.Name = "Organization"
elementOrg.SchemaTypeName = New XmlQualifiedName
("string", "http://www.w3.org/2001/XMLSchema")
The first statement from the above code fragment adds the element “elementOrg” to the schema (which we already created at the top). The second and third statements determine the characteristics of the element “elementOrg.” The second statement assigns the element name and the third defines the “data type” with respect to the definitions available at http://www.w3.org/2001/XMLSchema. Further proceeding, we have the following
Dim nsmgr As New XmlNamespaceManager(New NameTable())
nsmgr.AddNamespace("xs",
"http://www.w3.org/2001/XMLSchema")
The above two statements are used to define the namespace to which our schema is adhered. Further proceeding we have the following:
Dim sw As New IO.StringWriter
schema.Write(sw, nsmgr)
Me.TextBox1.Text = sw.ToString
The above code fragment creates a string writer where the schema is generated (using the namespace we defined earlier). Once the schema generation is complete we finally display it using the last line.
Next: Generating XML Schema dynamically using the .NET framework: complex type >>
More XML Articles
More By Jagadish Chaterjee