XmlSchemaGroupRef does not have the pre-compiled particle on it. Hence you need to look up the group from the RefName and then drill down further:
The following output is generated by the code sample below for the schema you posted:
Seq, All or Choice
XmlSchemaElement: Elm1
XmlSchemaElement: Elm2
Group ref
Seq, All or Choice
XmlSchemaElement: GO_Elm1
XmlSchemaElement: GO_Elm2
Group ref
Seq, All or Choice
XmlSchemaElement: SGO_Elm1
XmlSchemaElement: SGO_Elm2
Notice the two groupRefs printed.
private void PrintParticles(string url) {
XmlSchema schema = XmlSchema.Read(new XmlTextReader(url, new NameTable()), new ValidationEventHandler(ValidationCallback));
schema.Compile(new ValidationEventHandler(ValidationCallback));
foreach(XmlSchemaType type in schema.SchemaTypes.Values) {
XmlSchemaComplexType complexType = type as XmlSchemaComplexType;
if (complexType != null) {
TraverseParticle(complexType.Particle, schema);
}
}
foreach(XmlSchemaElement elem in schema.Elements) {
XmlSchemaComplexType complexType = elem.ElementType as XmlSchemaComplexType;
if (complexType != null && complexType.QualifiedName == XmlQualifiedName.Empty) { //Only local types of an element
TraverseParticle(complexType.Particle, schema);
}
}
}
private void TraverseParticle(XmlSchemaParticle particle, XmlSchema schema) {
if (particle is XmlSchemaElement) {
XmlSchemaElement elem = particle as XmlSchemaElement;
Console.WriteLine("XmlSchemaElement: " + elem.QualifiedName.ToString());
}
else if (particle is XmlSchemaGroupRef) {
XmlSchemaGroupRef groupRef = particle as XmlSchemaGroupRef;
Console.WriteLine("Group ref");
XmlSchemaGroup group = (XmlSchemaGroup)schema.Groups[groupRef.RefName]; //Get the group
TraverseParticle(group.Particle, schema);
}
else if (particle is XmlSchemaGroupBase) {
Console.WriteLine("Seq, All or Choice");
XmlSchemaGroupBase baseParticle = particle as XmlSchemaGroupBase;
foreach(XmlSchemaParticle subParticle in baseParticle.Items) {
TraverseParticle(subParticle, schema);
}
}
}
|