Generating XML Schema Dynamically Using VB.NET 2005: Essentials
(Page 1 of 4 )
This is the first 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.
There exist several articles on XML alone on this website. If you are new to XML Schema, I strongly suggest you go through my series “Designing your own XML Schema.” That series will introduce you to all the necessary concepts in designing XML schemas, even if you are a beginner.
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 compile 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
Generating XML Schema dynamically using the .NET framework: preliminaries
Not everyone knows that we can generate XML schema dynamically using the .NET framework. This happened to be the situation in one of my projects. And I just wanted to share my knowledge with you. I experienced few troubles when I ported my .NET 1.1 applications to .NET 2.0 (due to obsolescence). During this series, we shall explore and address the most important issues on this topic.
First of all, we need to import two namespaces, namely “System.Xml” and “System.Xml.Schema”. Both of them are available in the “System.Xml” assembly. So, we need to refer (using “Add Reference”) to “System.Xml” before importing the two namespaces.
For this sample, I just created a simple Windows application using Visual Studio with a single button and multi-lined textbox. When I hit the button it would generate some XML Schema in the multi-lined textbox. So, I suggest you design the same before you proceed with the code.
Now let us consider that I would like to have the following XML Schema to be generated:
<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Organization" type="xs:string" />
</xs:schema>
The above XML Schema simply declares a single element, “Organization.” To generate the above XML Schema dynamically, copy and paste the following code and execute it with the application:
Imports System
Imports System.Xml
Imports System.Xml.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 elementOrg As New XmlSchemaElement()
schema.Items.Add(elementOrg)
elementOrg.Name = "Organization"
elementOrg.SchemaTypeName = New XmlQualifiedName
("string", "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
That’s it. Just execute the application and hit the button. It will show the previous XML Schema in to the textbox. You can find an explanation for the above code in the next section.
Next: Understanding the dynamic generation of XML Schema using the .NET framework >>
More XML Articles
More By Jagadish Chaterjee