Generating XML Schema Dynamically Using VB.NET 2005: Annotations and Nesting Complex Types
This is the second article in a series concentrating on generating XML Schema dynamically using Visual Basic 2005. The series is mainly targeted at those who are familiar with XML, XML Schema and the .NET framework.
A downloadable file for this article is available here.
If you are new to this series, you can find the first article of this series here. If you are new to XML, there exist several articles on XML alone on this website (try “igrep”). If you are new to XML Schema, I strongly suggest you go through my series “Designing your own XML Schema.”
The entire solution for this article was developed using Visual Studio 2005 Professional Edition on Windows Server 2003 standard edition. Some examples in this series may not be compiled successfully using Visual studio 2003, as some of the features in .NET 1.1 turned out to be obsolete in .NET 2.0. For complete details you can refer to the link http://go.microsoft.com/fwlink/?linkid=14202. Please note that some of the examples in this series may not be practical. Those were designed only for explaining the particular concept.
Generate XML Schema dynamically using .NET framework – more practical complex type
Since I covered all the basics in my previous article, we shall now focus on more practical “complex types” in XML Schema. Let us consider the following XML Schema:
The above schema mainly contains a root element, “Organization.” Within the same element, we are trying to create as many “Employee” elements as possible. Every “Employee” element internally will have “ID” and “Name” elements. To generate the above XML Schema dynamically, we can consider the following code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox1.Text = ""
Dim schema As New XmlSchema()
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
Dim eEmployee As New XmlSchemaElement()
sqOrganization.Items.Add(eEmployee)
eEmployee.Name = "Employee"
eEmployee.MaxOccursString = "unbounded"
Dim ctEmployee As New XmlSchemaComplexType()
eEmployee.SchemaType = ctEmployee
Dim sqEmployee As New XmlSchemaSequence
ctEmployee.Particle = sqEmployee
Dim eID As New XmlSchemaElement()
sqEmployee.Items.Add(eID)
eID.Name = "ID"
eID.SchemaTypeName = New XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema")
Dim eName As New XmlSchemaElement()
sqEmployee.Items.Add(eName)
eName.Name = "Name"
eName.SchemaTypeName = New XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema")
Dim nsmgr As New XmlNamespaceManager(New NameTable())
I purposefully excluded “import” statements, because I already covered them in my previous article. You can find a step-by-step explanation of the above code in the next section.
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())
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.
Every element in an XML schema can be provided with some documentation and annotation. We can even have them dynamically created. Let us first consider the following XML Schema:
<xs:documentation>Employees list in the Pacific Northwest of US</xs:documentation>
</xs:annotation>
<xs:complexType />
</xs:element>
</xs:schema>
The above schema mainly contains a root element, “Organization.” You can also observe that “Organization” is currently a complex type. It is provided with some annotation with documentation. To generate the above XML Schema dynamically, we can consider the following complete code listing:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.TextBox1.Text = ""
Dim schema As New XmlSchema()
Dim eOrganization As New XmlSchemaElement
schema.Items.Add(eOrganization)
eOrganization.Name = "Organization"
Dim aOrganization As New XmlSchemaAnnotation
eOrganization.Annotation = aOrganization
Dim dOrganization As New XmlSchemaDocumentation
aOrganization.Items.Add(dOrganization)
dOrganization.Markup = TextToNodeArray("Employees list in the Pacific Northwest of US")
Dim ctOrganization As New XmlSchemaComplexType
eOrganization.SchemaType = ctOrganization
Dim nsmgr As New XmlNamespaceManager(New NameTable())
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. Further proceeding, we have the following:
Dim eOrganization As New XmlSchemaElement
schema.Items.Add(eOrganization)
eOrganization.Name = "Organization" . . .
Dim ctOrganization As New XmlSchemaComplexType
eOrganization.SchemaType = ctOrganization
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”.
Continuing on, we have the following:
Dim aOrganization As New XmlSchemaAnnotation
eOrganization.Annotation = aOrganization
Dim dOrganization As New XmlSchemaDocumentation
aOrganization.Items.Add(dOrganization)
dOrganization.Markup = TextToNodeArray("States in the Pacific Northwest of US")
The above code fragment first creates an annotation (or “<annotation>”) and adds the annotation to the “Organization” element. Once added, we then create a documentation element (or “<documentation>”) and add this to the annotation element. To have some information to place as part of the documentation, you can use the last statement. The “TextToNodeArray” is defined as follows:
Public Shared Function TextToNodeArray(ByVal text As String) As XmlNode()
Dim doc As New XmlDocument()
Return New XmlNode(0) {doc.CreateTextNode(text)}
End Function
The above function mainly accepts some message as a string and returns an array of “XmlNode”s. It is required for the “Markup” property of documentation, which accepts the same.
Next, we have the following code snippet:
Dim nsmgr As New XmlNamespaceManager(New NameTable())
The above two statements are used to define the namespace on which our schema gets 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 gets generated (using the namespace we defined earlier). Once the schema generation is complete we finally display it using the last line.
At the moment, I am not focusing on validating or compiling the XML Schema dynamically. We can have all of those features in the upcoming articles. Sign up for the newsletter to get yourself notified when they're published.
Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com