Your XML file is an XML Schema Definition (XSD) file that formally describe[s] the elements in an Extensible Markup Language (XML) document. You can load such a file into an XmlSchemaSet
schema collection, compile it, and then interrogate the schemas thereby defined to get your main element name and its properties.
Let's say that you have a string xsdString
that contains the XSD shown in your question. Then you can load it, compile it, and print out the complex elements and their properties as follows:
// Load the xsdString into an XmlSchemaSet.
// If loading from a file use a StreamReader rather than a StringReader
XmlSchemaSet schemaSet = new XmlSchemaSet();
using (var reader = new StringReader(xsdString))
{
schemaSet.Add(XmlSchema.Read(reader, null));
}
// Compile the schema
schemaSet.Compile();
// Iterate over the schemas
// Code adapted from https://learn.microsoft.com/en-us/dotnet/standard/data/xml/traversing-xml-schemas
foreach (XmlSchema schema in schemaSet.Schemas())
{
// Iterate over the complex types in the schema
foreach (XmlSchemaElement element in schema.Elements.Values)
{
var complexType = element.ElementSchemaType as XmlSchemaComplexType;
if (complexType == null)
continue;
Console.WriteLine("Complex element: {0}", element.Name);
// If the complex type has any attributes, get an enumerator
// and write each attribute name to the console.
if (complexType.AttributeUses.Count > 0)
{
var enumerator = complexType.AttributeUses.GetEnumerator();
while (enumerator.MoveNext())
{
var attribute = (XmlSchemaAttribute)enumerator.Value;
var name = attribute.Name;
var type = attribute.AttributeSchemaType.TypeCode;
Console.WriteLine(" Attribute {0}: {1}", name, type);
}
}
// Get the sequence particle of the complex type.
var sequence = complexType.ContentTypeParticle as XmlSchemaSequence;
if (sequence != null)
{
// Iterate over each XmlSchemaElement in the Items collection.
foreach (XmlSchemaElement childElement in sequence.Items)
{
var name = childElement.Name;
var type = childElement.ElementSchemaType.TypeCode;
Console.WriteLine(" Element {0}: {1}", name, type);
}
}
}
}
Which outputs
Complex element: Student
Element Id: Integer
Element Name: String
Element City: String
Note that the XSD type xs:integer
corresponds to an unbounded integer, not necessarily one that will fit in an Int32
or Int64
.
References:
Demo fiddle here.