I know, where is XmlSchema
class in dot net, but it does not allow to load file into it. Is there a clean solution to load xsd file and travel between elements?
views:
31answers:
2You need to use XmlSchemaSet
, e.g.:
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(targetNamespace, schemaUri);
schemaSet.Compile();
foreach(XmlSchemaElement element in schemaSet.GlobalElements.Values)
{
// do stuff...
}
EDIT: Sorry for not being clearer.
Where the comment says // do stuff...
, what you need to do is traverse the inherited types of each element, which are available under XmlSchemaElement.SchemaType
, and the inline type of the element, which is available under XmlSchemaElement.ElementSchemaType
.
The MSDN does contain all the the information you're after, but it is somewhat maze-like and requires digging around plus a bit of trial-and-error to work it out.
As per my comment, the following class from one of my open-source side-projects may be of use to you here:
http://bitbucket.org/philbooth/schemabrute/src/tip/LibSchemaBrute/Navigator.cs
I suggest checking out this tool: http://www.altova.com/xmlspy.html. You can create a data model from any XSD file. I've used it in many XML projects.