Sign In/My Account | View Cart  
advertisement

Article:
 The .NET Schema Object Model
Subject: Traversing the Schema Child with Group Reference
Date: 2003-08-01 14:02:57
From: Matt Frame
Response to: Traversing the Schema Child with Group Reference

Priya,


Sorry I forgot to append the test schema I am trying to use now with the same problem.


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:complexType name="TopLevel">
<xs:sequence>
<xs:element name="Elm1"/>
<xs:element name="Elm2"/>
<xs:group ref="GroupOne"/>
</xs:sequence>
</xs:complexType>
<xs:group name="GroupOne">
<xs:sequence>
<xs:element name="GO_Elm1"/>
<xs:element name="GO_Elm2"/>
<xs:group ref="SubGroupOne"/>
</xs:sequence>
</xs:group>
<xs:group name="SubGroupOne">
<xs:sequence>
<xs:element name="SGO_Elm1"/>
<xs:element name="SGO_Elm2"/>
</xs:sequence>
</xs:group>
</xs:schema>


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.


Thanks,


Matt


Previous Message Previous Message Move up to Parent Message Up Next Message No Next Message


Titles Only Titles Only Newest First
  • Traversing the Schema Child with Group Reference
    2003-08-01 15:57:36 Priyamvadha Lakshminarayanan

    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);
    }
    }
    }

    • Traversing the XML Schema
      2006-05-02 20:48:01 Forum

      Hi friends


      this is my xsd file


      <?xml version="1.0" encoding="utf-8"?>
      <xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/XMLSchema.xsd" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <!-- -->
      <xs:element name="Plan">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="Patient_Data">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="Name_of_Patient" type="xs:string" />
      <xs:element name="Patient_ID" type="xs:string" />
      <xs:element name="Clinical_Record" type="xs:string" />
      <xs:element name="Site_of_Target_Vol" type="xs:string" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      <xs:element name="General_Data">
      <xs:complexType>
      <xs:sequence>
      <xs:element name="Planned_on" type="xs:date" />
      <xs:element name="Planned_by" type="xs:string" />
      <xs:element name="Date_of_Protocol" type="xs:dateTime" />
      <xs:element name="Number_of_Pages_Protocol" type="xs:int" />
      <xs:element name="Image_Series_ID" type="xs:int" />
      <xs:element name="Target_Point_Mode" type="xs:string" />
      <xs:element name="Device_Coord_Syst" type="xs:string" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs: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


      Thanks in advance


      Regards


    • Traversing the Schema Child with Group Reference
      2003-08-02 22:36:39 Matt Frame

      Priya,


      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,


      Matt


Sponsored By: