Generating XML Schema Dynamically Using VB.NET 2005: Essentials - Understanding the dynamic generation of complex type in XML Schema
(Page 4 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, and so on to the same schema object. Further proceeding, we have the following:
Dim eEmployee As New XmlSchemaElement()
schema.Items.Add(eEmployee)
eEmployee.Name = "Employee"
The above code fragment simply creates a new element called “Employee” and adds it to the schema generator. Further proceeding we have:
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 “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 the “Employee” element) and a “sequence particle” (which means that the elements should be provided in an orderly fashion). Further proceeding, we have the following:
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 the nested elements inside it (not every particle may contain nested elements as well). 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 to which our schema gets adhered. Proceeding further 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 is generated (using the namespace we defined earlier). Once the schema generation is complete we finally display it using the last line.
I hope that this article gives you a kick-start in generating dynamic XML Schemas using the .NET framework. Another issue to pay attention to is that the examples I used in this demonstration may not be helpful in practical (or production) scenarios. I strongly suggest you have a proper working draft design of the XML Schema you wanted to generate, and finally generate it using the code I present in this series. A working draft may not be necessary if you want to generate the XML Schema from existing relational databases or other, similar kinds of structured information sources.
Any comments, suggestions, feedback, bugs, errors, enhancements are highly appreciated at jag_chat@yahoo.com
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |