Generating XML Schema Dynamically Using VB.NET 2005: Annotations and Nesting Complex Types - Understanding the generation of XML Schema annotations dynamically using the .NET framework
(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, etc to the same schema object. Further proceeding, we have the following:
Dim eOrganization As New XmlSchemaElement
schema.Items.Add(eOrganization)
eOrganization.Name = "Organization"
.
.
.
Dim ctOrganization As New XmlSchemaComplexType
eOrganization.SchemaType = ctOrganization
The above code fragment creates the root element “Organization.” As “Organization” internally may have some other elements, we make it of the complex type using “ctOrganization”.
Continuing on, we have the following:
Dim aOrganization As New XmlSchemaAnnotation
eOrganization.Annotation = aOrganization
Dim dOrganization As New XmlSchemaDocumentation
aOrganization.Items.Add(dOrganization)
dOrganization.Markup = TextToNodeArray("States in the Pacific Northwest of US")
The above code fragment first creates an annotation (or “<annotation>”) and adds the annotation to the “Organization” element. Once added, we then create a documentation element (or “<documentation>”) and add this to the annotation element. To have some information to place as part of the documentation, you can use the last statement. The “TextToNodeArray” is defined as follows:
Public Shared Function TextToNodeArray(ByVal text As String) As XmlNode()
Dim doc As New XmlDocument()
Return New XmlNode(0) {doc.CreateTextNode(text)}
End Function
The above function mainly accepts some message as a string and returns an array of “XmlNode”s. It is required for the “Markup” property of documentation, which accepts the same.
Next, we have the following code snippet:
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 on which our schema gets adhered. Further proceeding 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 gets generated (using the namespace we defined earlier). Once the schema generation is complete we finally display it using the last line.
At the moment, I am not focusing on validating or compiling the XML Schema dynamically. We can have all of those features in the upcoming articles. Sign up for the newsletter to get yourself notified when they're published.
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. |