![]() |
Platforms, Frameworks & Libraries »
.NET Framework »
General
Intermediate
An XML Schema Definition (XSD) EditorBy Marc CliftonAn editor capable of producing common XSD documents |
C#, Windows, .NET 1.0, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search |
|
|
|
||||||||||||||||

This article presents an editor for XML Schema Definition (XSD) documents, implemented in C# using .NET's XmlSchema classes. Searching the Internet, I found only one such editor: XML Architect[^]. I found that the editor included in VS.NET to be unecessarily similar to a database schema editor. My personal opinion is that XML is more hierarchical and less relational than a database schema, and so I feel that a tree view is a better presentation of the schema.
The editor has been tested by using it to create the Purchase Order XSD that is used in the XML Schema Primer[^].XmlSchemaFacet and XmlSchemaNumericFacet classes
XmlSchemaType classes
XmlSchemaObject classes, except for XmlSchemaAnnotated
XmlSchemaSimpleTypeRestriction within the XmlSchemaSimpleTypeContent class
XmlSchemaAttribute class
These classes provide the basic functionality for designing an XSD document. There are many other classes in the SOM that this editor does not currently support. I will be adding support for these additional classes as required.
XmlSchemaComplexType attributes are contained in the Attribute member, while the sub-elements are contained in an XmlSchemaSequence object which is an object assigned to the Particle member. Furthermore, given a schema object, it is impossible to determine the parent schema element. This all makes for some complicated rules for inserting and removing schema objects.
The Remove function in the editor illustrates this:
// The parent type determines from what list the selected item must be removed. // Use the image index in the tree view to figure out the parent type. private void mnuRemoveNode_Click(object sender, System.EventArgs e) { TreeNode tnParent=tvSchema.SelectedNode.Parent; XmlSchemaObject obj=tvSchema.SelectedNode.Tag as XmlSchemaObject; bool success=false; // if the node to remove has a parent and is of an XmlSchemaObject type... if ( (tnParent != null) && (obj != null) ) { // look at the tree node image index to figure out what the parent is!Note that I get the parent from the tree view, not the schema!
And I use the tree view's image to determine the parent type!
Schema root objects are simply removed:
switch ((TreeViewImages)tnParent.ImageIndex) { // if the parent is the schema root: case TreeViewImages.Schema: { // remove the object from the schema and from the global list schema.Items.Remove(obj); int idx=cbGlobalTypes.FindStringExact(tvSchema.SelectedNode.Text); if (idx != -1) { cbGlobalTypes.Items.RemoveAt(idx); } success=true; break; }Schema annotation objects removed from the
XmlSchemaAnnotation Items member: // if the parent is an annotation type case TreeViewImages.Annotation: { XmlSchemaAnnotation annot=tnParent.Tag as XmlSchemaAnnotation; if (annot != null) { annot.Items.Remove(obj); success=true; } break; }If the parent is an
XmlSchemaSimpleType, then I have to remove the object from different places, depending on the object type--XmlSchemaAnnotation or XmlSchemaFacet: // if the parent is a simple type case TreeViewImages.SimpleType: { // a simple type can have an annotation or a facet type as children XmlSchemaSimpleType st=tnParent.Tag as XmlSchemaSimpleType; if (obj is XmlSchemaAnnotation) { // remove from annotation list if it's an annotation type st.Annotation.Items.Remove(obj); success=true; } else if (obj is XmlSchemaFacet) { XmlSchemaSimpleTypeRestriction rest=st.Content as XmlSchemaSimpleTypeRestriction; if (rest != null) { // remove from facet list if it's a facet type rest.Facets.Remove(obj); success=true; } } break; }A parent cannot be an
XmlSchemaElement, because any element with sub-elements is an XmlSchemaComplexType, which can contain an XmlSchemaAttribute or XmlSchemaElement objects as part of an XmlSchemaSequence Items collection. // if the parent is a complex type... case TreeViewImages.ComplexType: { XmlSchemaComplexType ct=tnParent.Tag as XmlSchemaComplexType; if (ct != null) { // then we are removing an attribute if (obj is XmlSchemaAttribute) { ct.Attributes.Remove(obj); success=true; } // or an annotation else if (obj is XmlSchemaAnnotation) { ct.Annotation.Items.Remove(obj); success=true; } // or an element type else { XmlSchemaSequence seq=ct.Particle as XmlSchemaSequence; if (seq != null) { seq.Items.Remove(obj); success=true; } } } break; } } } ... }As you can see, this is not trivial.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Oct 2003 Editor: Marc Clifton |
Copyright 2003 by Marc Clifton Everything else Copyright © CodeProject, 1999-2009 Web12 | Advertise on the Code Project |