Generating XML Schema Dynamically Using VB.NET 2005: Working With Attributes - Generate XML Schema attributes dynamically using the .NET framework: explanation
(Page 5 of 5 )
This section explains the code fragment listed in the previous section. Let us examine it part by part. I am excluding some of the parts which are already explained in previous articles (and even above).
From the following code, you can observe that I removed the sequence particle related to the “Employee” element. It is just maintaining its own complex type:
Dim eEmployee As New XmlSchemaElement()
sqOrganization.Items.Add(eEmployee)
eEmployee.Name = "Employee"
eEmployee.MaxOccursString = "unbounded"
Dim ctEmployee As New XmlSchemaComplexType()
eEmployee.SchemaType = ctEmployee
We add the attribute “ID” to the “Employee” element as follows:
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")
You should also observe that attributes are to be added to a complex type and never to a sequence type. According to the above code fragment, we create a new XML schema attribute (“eID”) and add it to the list of attributes related to the complex type of “Employee” element (second statement in the above code). I wanted to make sure that “ID” is a compulsory attribute for every “Employee” element (which is enforced in the fourth statement). Similarly, I even defined “Name” and “Age” (but observe that “Age” is not enforced with “XmlSchemaUse.Required”).
We shall focus on “restrictions” in the up coming articles. So, stay tuned by signing up the newsletter. 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. |