Sign In/My Account | View Cart  
advertisement

Article:
 The .NET Schema Object Model
Subject: Targeting a nested child element
Date: 2003-02-25 14:00:11
From: Priyamvadha Lakshminarayanan
Response to: Targeting a nested child element

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


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-05-29 13:22:20 Carol Skelly [Reply]

    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 [Reply]

      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 [Reply]

      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: