Generating Restrictions in XML Schema Dynamically Using VB.NET 2005: Preliminaries - Restrictions (or constraints) in XML Schema: VB.NET sample
(Page 2 of 5 )
In the previous section, I already listed a schema which we would like to generate dynamically. The following is the complete VB.NET code to generate the previous schema:
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 eName As New XmlSchemaElement()
sqEmployee.Items.Add(eName)
eName.Name = "Name"
eName.SchemaTypeName = New XmlQualifiedName("string",
"http://www.w3.org/2001/XMLSchema")
Dim eAge As New XmlSchemaElement()
sqEmployee.Items.Add(eAge)
eAge.Name = "Age"
Dim simpleType As New XmlSchemaSimpleType()
eAge.SchemaType = simpleType
Dim restriction As New XmlSchemaSimpleTypeRestriction()
simpleType.Content = restriction
restriction.BaseTypeName = New XmlQualifiedName
("integer", "http://www.w3.org/2001/XMLSchema")
Dim maxInclusive As New XmlSchemaMaxInclusiveFacet()
restriction.Facets.Add(maxInclusive)
maxInclusive.Value = "100"
Dim eID As New XmlSchemaAttribute()
ctEmployee.Attributes.Add(eID)
eID.Name = "ID"
eID.Use = XmlSchemaUse.Required
eID.SchemaTypeName = New XmlQualifiedName("int",
"http://www.w3.org/2001/XMLSchema")
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
I purposefully excluded the “import” statements, because I already covered them in my previous articles. You can find a step-by-step explanation of the above code in the next section.
Next: Restrictions (or constraints) in XML Schema: explanation >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee