Sign In/My Account | View Cart  
advertisement

Article:
 The .NET Schema Object Model
Subject: Targeting a nested child element
Date: 2003-02-24 12:03:30
From: Priyamvadha Lakshminarayanan
Response to: Targeting a nested child element

The Elements property returns all top-level elements added to the schema. To access the child elements, you need to access the complex type of the element and its LocalElements property.


foreach (XmlSchemaElement parentElem in schema.Elements.Values) {
XmlSchemaComplexType ct = parentElem.ElementType as XmlSchemaComplexType;
if (ct != null) {
foreach(XmlSchemaElement childElem in ct.LocalElements.Values) {
Console.WriteLine(childElem.QualifiedName.ToString());
}
}
}


Thanks,
Priya


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


Titles Only Titles Only Newest First
  • Targeting a nested child element
    2003-02-25 06:10:51 Wendy Attenberger

    I used the following line in vb.net:


    Dim ct As XmlSchemaComplexType = parentElement.ElementType


    to replace this one in c#:


    XmlSchemaComplexType ct = parentElem.ElementType as XmlSchemaComplexType


    On the next line, I try to access the LocalElements collection - but I don't see a LocalElements collection in my intellisense. (I see Particle, Qualified Name, etc.) I don't think I have the right syntax in vb.net.


    You wouldn't have to have any ideas on the syntax for vb.net? Thanks again...this has been extremely helpful. I have found very little sample code in this area.

    • Targeting a nested child element
      2003-02-25 14:00:11 Priyamvadha Lakshminarayanan

      My mistake, the LocalElements property is not available in Version 1 of the .NET Framework.


      Try the following sample instead:


      foreach (XmlSchemaElement parentElement in schema.Elements.Values) {


      XmlSchemaComplexType ct = parentElement.ElementType as XmlSchemaComplexType; //Casting to complex type
      if (ct != null) {
      XmlSchemaSequence seq = (XmlSchemaSequence)ct.ContentTypeParticle; //Assuming it’s a sequence of elements
      foreach(XmlSchemaParticle p in seq.Items) {
      XmlSchemaElement elem = p as XmlSchemaElement; //Check if particle in seq is XmlSchemaElement
      if (elem != null)
      Console.WriteLine(elem.QualifiedName.ToString());
      }
      }


      Thanks,
      Priya

      • Targeting a nested child element
        2003-05-29 13:22:20 Carol Skelly

        Hi..


        I also need to accomplish this in Vb.Net -- has anyone been successful with this?


        Also -- as you said the post-complilation Elements collection only contains top-level 'elements' -- so wouldn't we need to examine the Items collection instead to find any 'complextypes' or 'simpletypes'? ie:


        foreach (XmlSchemaElement parentElement in schema.Items) {


        Thanks for this great article! And please let me know if you can shed any light on the matter of traversing nested schema elements. Thanks!




        • Targeting a nested child element
          2004-11-15 06:36:28 Qoheleth

          VB.NET version of the above code would be


          Dim parentElement As XmlSchemaElement
          For Each parentElement In schema.Elements.Values
          'Try Casting to complex type
          Dim ct As XmlSchemaComplexType
          Try
          ct = DirectCast(parentElement.ElementType, XmlSchemaComplexType)
          Catch ex As InvalidCastException
          ct = Nothing
          End Try
          If Not ct Is Nothing Then
          Dim seq As XmlSchemaSequence = DirectCast(ct.ContentTypeParticle, XmlSchemaSequence)


          'Assuming it’s a sequence of elements
          Dim p As XmlSchemaParticle
          For Each p In seq.Items
          'Check if particle in seq is XmlSchemaElement
          Dim elem As XmlSchemaElement
          Try
          elem = DirectCast(p, XmlSchemaElement)
          Catch ex As InvalidCastException
          elem = Nothing
          End Try


          If Not elem Is Nothing Then
          Console.WriteLine(elem.QualifiedName.ToString());
          End If
          Next
          End If
          Next





        • Targeting a nested child element
          2003-05-29 14:53:04 Priyamvadha Lakshminarayanan

          To access nested elements, you would need to traverse down the content model of the parent element as shown in my previous post.


          The elements in the post-compilation Elements collection each have an ElementType property which exposes the compiled complex type or simple type of that element. To get to the child elements, you need to navigate down the complex type.


          The items collection is used to add items to the schema prior to compilation.





Sponsored By: