Generating XML Schema Dynamically Using VB.NET 2005: Annotations and Nesting Complex Types - Understanding the dynamic generation of complex type in XML Schema
(Page 2 of 4 )
This section explains the code fragment listed in the previous section. Let us examine it part by part. 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 starts our XML Schema. Once a schema object is created, we can start adding our own elements, attributes, processing instructions, comments, etc to the same schema object. Continuing on, we have the following:
Dim eOrganization As New XmlSchemaElement
schema.Items.Add(eOrganization)
eOrganization.Name = "Organization"
Dim ctOrganization As New XmlSchemaComplexType
eOrganization.SchemaType = ctOrganization
Dim sqOrganization As New XmlSchemaSequence
ctOrganization.Particle = sqOrganization
The above code fragment creates the root element “Organization.” As “Organization” internally may have some other elements, we make it of the complex type using “ctOrganization.” We maintain sequence as the type of particle for the complex type.
Dim eEmployee As New XmlSchemaElement()
sqOrganization.Items.Add(eEmployee)
eEmployee.Name = "Employee"
eEmployee.MaxOccursString = "unbounded"
The above code fragment simply creates a new element called “Employee” and adds it to the “Organization” sequence particle. You can also observe the “unbounded,” which allows the element “Employee” to be repeated any number of times. Next we have the following:
Dim ctEmployee As New XmlSchemaComplexType()
eEmployee.SchemaType = ctEmployee
Dim sqEmployee As New XmlSchemaSequence
ctEmployee.Particle = sqEmployee
As “Employee” is of the “complex type” (and not the “simple type”), we need to specify this point. A complex type should also be joined with a “particle.” The “particle” could be any of “All,” “Sequence,” “Choice” and so on (which I already explained in my “Designing your own XML Schema” series).
The above code fragment creates a new complex type (for “Employee” element) and a “sequence particle” (means that the elements should be provided in an orderly fashion). Proceeding to the next fragment, we have:
Dim eID As New XmlSchemaElement()
sqEmployee.Items.Add(eID)
eID.Name = "ID"
eID.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
A “particle” generally contains some nested elements inside it (not every particle may contain nested elements). In our case, the “Employee” element needs to be nested with two more elements (child elements), “ID” and “Name.”
The above code fragment creates a new element, names it “ID,” defines it as an “integer” data type, and finally adds it to the “sequence particle.” Similarly, we add the “Name” element as follows:
Dim eName As New XmlSchemaElement()
sqEmployee.Items.Add(eName)
eName.Name = "Name"
eName.SchemaTypeName = New XmlQualifiedName("string", "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 on which our schema gets adhered. Finally, we have this snippet:
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 gets generated (using the namespace we defined earlier). Once the schema generation is complete we finally display it using the last line.
Next: Generate XML Schema annotations dynamically using the .NET framework >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee