views:

31

answers:

2

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?

A: 

You 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

Phil Booth
Ok, I got all global elements, but how can I get child elements of this global elements? I tried everything, but it seems what XmlSchemaElement has not have references on it child elements.
Yuri
Okay, that is too complicated a question to answer fully in one of these comments, but essentially you need to analyse the `SchemaType` and `ElementSchemaType` members of `XmlSchemaElement`. The MSDN does contain all the details, but you need to do some digging for it to become clear. If you post another question I can help more, otherwise you could take a look at one of my little side-projects that does this stuff. The class you should look at is called LibSchemaBrute::Navigator ~ http://bitbucket.org/philbooth/schemabrute/src/tip/LibSchemaBrute/Navigator.cs
Phil Booth
Big thanks, I'll try to reuse your code.
Yuri
A: 

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.

Icono123
Sorry, I don't need any tool, I need programmatic advice.
Yuri
I'm sorry you don't need a tool. I realize you don't necessarily need tools to complete a task. Tools allow you to work faster and more efficiently. I would suggest anyone working with XML try the tool because it will save a lot of time and is very useful. Thanks
Icono123