My ultimate goal is to traverse an XSD document and display a TreeView (Silverlight 4.0) representation of all the XML elements in order as described by the XSD document.
So, if I had an XSD file that describes a set of elements that would look like
<a>
<b att1="foo" att2="foo2"/>
<c />
<d />
</a>
then I would want to display a TreeView like:
a
|_b
| |_att1
| |_att2
| |_c
|_d
I don’t care if the XSD document allows for multiple occurrences or optional elements and attributes. I want to list elements that occurr multiple times only once, and all required and optional elements/attributes should appear. For example a schema that allows
<a>
<b att1="foo" att2="this one was optional"/>
<c />
<d />
<d />
</a>
should still return the same TreeView I showed previously.
However, I am having trouble understanding the best way to go about doing this. I started to use an XmlReader to run through the xsd document, but then realized it can become complicated dealing with the references across files… e.g. elements that have “ref” or “type” attributes….or “extension” elements.
So, I created a WCF service so I could use the XmlSchema class and see what was there. I wrote the following code:
XmlTextReader reader = new XmlTextReader("http://myXsdLocation.com/SalesOrder.xsd");
XmlSchema myschema = XmlSchema.Read(reader, ValidationCallback);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(myschema);
schemaSet.Compile();
Debugging through that code, the schemaSet count increases to 8 after schemaSet.Compile(), which I assume means that with all the “include” references in the initial xsd file, as well as in the referenced xsd files, there are 8 xsd files altogether for the initial one to make sense.
But from noodling around in the schemaSet objects, I haven’t found a way to elegantly traverse through the entire schema as if it were one giant XSD file.
Does anyone know a way to accomplish what I am trying to do? I just want to be able to pull each element in the order it would appear as one giant XSD file, and any attributes attached to the element and give the data to my TreeView. I’m okay with with the UI stuff, but need to know if it’s better to write my own parser with the XmlReader or to use the XmlSchema and related classes. I’ve found similar questions throughout StackOverflow, but most of it referenced code gen tools or are unrelated to what I am looking for. I want to develop something to take any xsd and produce the tree I mentioned earlier.
I’m not familiar with the XmlSchema API you refer to.
Another way to tackle this would be to use the Saxon schema processor to generate an SCM file, which is an XML representation of the schema component model – effectively the compiled schema in “canonical” form, which is an XML representation that is much easier to navigate than the raw XSD files.