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());
}
}
}
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.
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());
}
}
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!
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 its 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
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.