Group references are resolved during compilation. If you want access to them, you should use complexType.Particle property which is the pre-compiled property instead of complexType.ContentTypeParticle which is a post-compilation property.
For the following schema,
<xsd:schema attributeFormDefault="qualified" targetNamespace="ns1" xmlns:x="ns1" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
If you iterate over the schema.SchemaTypes
and get the complex type CT,
complexType.Particle will be a XmlSchemaSequence with 3 items:
System.Xml.Schema.XmlSchemaElement (E1)
System.Xml.Schema.XmlSchemaGroupRef (G1)
System.Xml.Schema.XmlSchemaElement (E2)
complexType.ContentTypeParticle will be a XmlSchemaSequence with 3 items (the group ref and pointless sequence has been resolved):
System.Xml.Schema.XmlSchemaElement (E1)
System.Xml.Schema.XmlSchemaElement (GE1)
System.Xml.Schema.XmlSchemaElement (E2)
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.
Thanks so much for getting back with me. I have to admit I am new to XML Schemas and the SOM. I see a big difference in the way you created a schema and the way I had, of course I am using XMLSPy and I am new at this. Here is a sample of what I had put together.
This schema doesn't have any schema types so I guess that is why I was having such a hard time traversing the tree, although I was using the Particle and not the ContentParticle.
I am still having difficulty putting the correct code together to traverse the tree using SchemaTypes. If you have a sample that would be great if you don't that's fine to. I just hate that I have spent a week traversing the tree one way only to find that was completely wrong and now I have to learn how to do it differently.