As you can see the first group has a reference to a another group, here SubGroupOne, this is the level I am trying to get an cannot. I cannot find the SubGroupOne name or any information about it when traversing the tree.
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);
}
}
}
i want to display or print all the elements in the schema., the elements are like "plan" under this "Patient_data and General_data" and under patient_data it has some sub elements. How can i traverse all the elemetns in the file. I tried like this
XmlTextReader reader = new XmlTextReader(@"E:\XmlTest\Test\PatientData.xsd");
try
{
XmlSchema schema = XmlSchema.Read(reader, null);
schema.Compile(null);
if (schema.IsCompiled)
{
MessageBox.Show("Inside");
foreach (XmlSchemaElement parentElement in schema.Elements.Values)
{
MessageBox.Show(parentElement.Name.ToString());
XmlSchemaComplexType ct = parentElement.ElementType as XmlSchemaComplexType; //Casting to complex type
if (ct != null)
{
XmlSchemaSequence seq = (XmlSchemaSequence)ct.ContentTypeParticle;
foreach (XmlSchemaElement elem in seq.Items)
{
MessageBox.Show(elem.Name.ToString());
}
}
}
it displays only plan and patient_data and General_data it is not displaying sub elements. Can u please tell me where the problem is? share ur ideas
Thank you so much for helping me out with this. I am getting a much better understanding of schema's and the SOM because of your help. I have one last question though.
Is it possible to be in SubGroupOne and find out which parent you came from? I guess I am asking can you traverse the tree in reverse if need be.