How can I load .xsd file to track relationships(nesting) between elements?
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 b开发者_高级运维etween elements?
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
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.
精彩评论