Generating XML Schema Dynamically Using VB.NET 2005: Working With Attributes - Generate more XML Schema attributes dynamically using .NET framework: a sample schema and XML
(Page 4 of 5 )
Let us consider the following situation for an XML document:
<?xml version="1.0" encoding="UTF-8"?>
<Organization xmlns:xsi="http://www.w3.org/2001/XMLSchema-
instance" xsi:noNamespaceSchemaLocation="C:\Documents and
Settings\Administrator\Desktop\ForDec\ForDec\XSLT\XMLExamples\02
\Sample.xsd">
<Employee ID="1001" Name="Jag" Age="27" />
<Employee ID="1002" Name="Winner" Age="20" />
<Employee ID="1003" Name="Dhan" />
<Employee ID="1004" Name="aaa" />
<Employee ID="1005" Name="bbb" Age="72" />
<Employee ID="1006" Name="ccc" />
</Organization>
According to the above, we would have a single “Organization” element nested with several “Employee” elements. Even though the “Employee” element is of the complex type, it is trying to accept all of the information in the form of attributes (rather than child elements). This is quite different from the previous scenario. Another important issue is that the “Age” attribute is not compulsory (observe carefully the above XML document).
Now, how about the XML Schema for the above XML document? It would look something like the following:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Organization">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" name="Employee">
<xs:complexType>
<xs:attribute name="ID" type="xs:int"
use="required" />
<xs:attribute name="Name" type="xs:string"
use="required" />
<xs:attribute name="Age" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Generate more XML Schema attributes dynamically using the .NET framework: source code
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 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 eName As New XmlSchemaAttribute()
ctEmployee.Attributes.Add(eName)
eName.Name = "Name"
eName.Use = XmlSchemaUse.Required
eName.SchemaTypeName = New XmlQualifiedName("string",
"http://www.w3.org/2001/XMLSchema")
Dim eAge As New XmlSchemaAttribute()
ctEmployee.Attributes.Add(eAge)
eAge.Name = "Age"
eAge.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
You can find a step-by-step explanation of the above code in the next section.
Next: Generate XML Schema attributes dynamically using the .NET framework: explanation >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee