Sign In/My Account | View Cart  
advertisement

Article:
 The .NET Schema Object Model
Subject: Targeting a nested child element
Date: 2003-02-24 10:21:26
From: Wendy Attenberger

We're trying to follow your example on page 2 of adding an element. We're doing this in memory - adding some complex types, compiling, then trying to go back and access those elements to add the necessary children. We don't want to save to disk until we've added all the children of the children, etc.


In using the loop -
For Each parentElement In schema.Elements.Values
Next
-
we can only get to the root element.


Sample portion of our dynamic schema is included below - only element we can get to is Orders...can't get to the child Order element or the child Customer element. We did a print out of the schema.Elements.Values.Count throughout our loop and it is only 1 - but this is what it prints out below.


<?xml version="1.0" encoding="utf-16"?>
<xs:schema elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Orders">
<xs:complexType>
<xs:sequence>
<xs:element name="Order">
<xs:complexType>
<xs:sequence />
</xs:complexType>
</xs:element>
<xs:element name="Customer">
<xs:complexType>
<xs:sequence />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


--Note we also tried the items collection before compiling but it only gets the "top" level items...it doesn't read/find nested items/elements. I found that detail on a Microsoft webcast. I guess this just gets top level elements also...??


Thanks.


Sincerely,
Wendy Attenberger


No Previous Message Previous Message   Next Message Next Message


Titles Only Titles Only Newest First
  • Targeting a nested child element
    2003-02-24 12:03:30 Priyamvadha Lakshminarayanan

    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

    • 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: