Generating XML Schema Dynamically Using VB.NET 2005: Annotations and Nesting Complex Types - Generate XML Schema annotations dynamically using the .NET framework
(Page 3 of 4 )
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:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Organization">
<xs:annotation>
<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())
nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema")
Dim sw As New IO.StringWriter
schema.Write(sw, nsmgr)
Me.TextBox1.Text = sw.ToString
End Sub
Public Shared Function TextToNodeArray(ByVal text As String) As XmlNode()
Dim doc As New XmlDocument()
Return New XmlNode(0) {doc.CreateTextNode(text)}
End Function
You can find a step-by-step explanation of the above code in the next section.
Next: Understanding the generation of XML Schema annotations dynamically using the .NET framework >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee