XmlSchema

XmlSchema in the .NET 1.1 framework is so difficult to use. I want to traverse an XSD. I have code to do it, taken and modified from some sample code I found online. Once I load and complile the schema, I traverse without issue. When I come across a complexType that has a complexContent node, it doesn't output anything relating to that. I cannot get it to traverse the elements within that node.

My problem is also that unless I explicity process the ContentModel and traverse that undet the extension, I don't get the attributes (baseID, etc).

If you could email me back atpnkfloydlunatic@netscape.(romovethis)net I would really appreciate it!! Thanks!

Brian
Here is the code. Assume I am starting at the root element, which I am doing.

private void writeAttributes(XmlSchemaObjectCollection attributes)
{
XmlSchemaObjectEnumerator ienum = (XmlSchemaObjectEnumerator)attributes.GetEnumerator();
while (ienum.MoveNext())
{
if ( ienum.Current is XmlSchemaAttribute )
{
XmlSchemaAttribute attr = ienum.Current as XmlSchemaAttribute;
string generatedData = generateData(attr.SchemaTypeName.Name);

System.Diagnostics.Debug.WriteLine(attr.QualifiedName.Name + "=" + generatedData);

//m_xmlWriter.WriteAttributeString(attr.QualifiedName.Name, attr.QualifiedName.Namespace, generatedData);
}
}
}


private void processComplexTypes ( XmlSchemaComplexType complexType )
{
if ( complexType == null )
return;

traverseParticle(complexType.ContentTypeParticle);

if ( complexType.Attributes.Count > 0 )
{
writeAttributes(complexType.Attributes);
}
}

private void processElement( XmlSchemaElement element )
{
if (element.RefName.IsEmpty)
{
System.Diagnostics.Debug.WriteLine("Element: " + element.Name);

if ( element.ElementType is XmlSchemaComplexType )
{
XmlSchemaComplexType complexType = element.ElementType as XmlSchemaComplexType;
processComplexTypes(complexType);
}
}
}

private void traverseParticle( XmlSchemaParticle particle )
{
if ( particle is XmlSchemaElement )
{
XmlSchemaElement elementType = particle as XmlSchemaElement;
processElement(elementType);
}
else if ( particle is XmlSchemaGroupBase )
{
//xs:all, xs:choice, xs:sequence
XmlSchemaGroupBase baseParticle = particle as XmlSchemaGroupBase;
foreach ( XmlSchemaParticle subParticle in baseParticle.Items )
{
traverseParticle(subParticle);
}
}
}
Here is the output
( note, AirlineSpecificProperties and properties are part of the 'TopLevelObject' base. It doesn't output BaseAirports, ReserveCriteriaList, and the attributes, etc.

Element: CrewBase
Element: AirlineSpecificProperties
Element: Property
name=test
value=test

Here is a sample of the XML

<xs:complexType name="CrewBase" abstract="false" mixed="false">
<xs:complexContent mixed="false">
<xs:extension base="TopLevelObject">
<xs:all>
<xs:element name="BaseAirports" type="AirportRefList" nillable="false" minOccurs="0"/>
<xs:element name="ReserveCriteriaList" type="ReserveCriteriaList" abstract="false" nillable="false" minOccurs="0"/>
</xs:all>
<xs:attribute name="baseID" type="TopLevelObjectID" use="required"/>
<xs:attribute name="city" type="xs:string" use="optional"/>
<xs:attribute name="code" type="xs:string" use="required"/>
<xs:attribute name="country" type="xs:string" use="optional"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>

[8738 byte] By [Pink] at [2008-2-17]
# 1

I need more of your schema to be able to help you - please post the whole thing or at least the definitions referenced by the above fragment including TopLevelObject, BaseAirports, ReserveCriteriaList, etc. Thanks.

ChrisLovett at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 2

Here is the request you wanted. The schema is quite large, so maybe we can avoid posting the whole thing if this is enough information. This is in addition to the XML in my initial post.

Thanks again.
Brian
--

<xs:complexType name="Properties" abstract="false" mixed="false">
<xs:sequence>
<xs:element name="Property" type="Property" nillable="false" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Property" abstract="false" mixed="false">
<xs:attribute name="name" type="xs:string" use="required"/>
<xs:attribute name="value" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="TopLevelObject" abstract="true" mixed="false">
<xs:annotation>
<xs:documentation>An abstract base type for data common to all descendants.</xs:documentation>
</xs:annotation>
<xs:all minOccurs="0">
<xs:element name="AirlineSpecificProperties" type="Properties" nillable="false" minOccurs="0">
<xs:annotation>
<xs:documentation>A collection of name/value pairs suitable for passing airline-specific data throughout the system.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:all>
</xs:complexType>
<xs:complexType name="AirportRef" abstract="false" mixed="false">
<xs:attribute name="airport" type="xs:IDREF" use="optional">
<xs:annotation>
<xs:documentation>intra-document Airport object reference.</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="airportID" type="TopLevelObjectRef" use="required">
<xs:annotation>
<xs:documentation>Key into Airport.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
<xs:complexType name="AirportRefList" abstract="false" mixed="false">
<xs:sequence>
<xs:element name="AirportRef" type="AirportRef" nillable="false" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ReserveCriteria" abstract="false" mixed="false">
<xs:all>
<xs:element name="Fleets" type="FleetRefList" abstract="false" nillable="false" minOccurs="0"/>
<xs:element name="FlightClasses" type="FlightClassList" abstract="false" nillable="false" minOccurs="0"/>
<xs:element name="FlightTypes" type="FlightTypeList" abstract="false" nillable="false" minOccurs="0"/>
<xs:element name="MarketCharacteristics" type="MarketCharacteristics" abstract="false" nillable="false" minOccurs="0"/>
</xs:all>
<xs:attribute name="positionType" type="PositionTypeEnumeration" use="required"/>
</xs:complexType>
<xs:complexType name="ReserveCriteriaList" abstract="false" mixed="false">
<xs:sequence>
<xs:element name="ReserveCriteria" type="ReserveCriteria" nillable="false" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>

Pink at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...
# 3
When I load this into the XML Text Editor I get errors:

Warning 1 'all' is not the only particle in a group, or is being used as an extension. d:\temp\XslProject\schema.xsd 79 4 XslProject
Warning 2 'all' is not the only particle in a group, or is being used as an extension. d:\temp\XslProject\schema.xsd 27 6 XslProject
Warning 3 'all' is not the only particle in a group, or is being used as an extension. d:\temp\XslProject\schema.xsd 82 10 XslProject

ChrisLovett at 2007-9-9 > top of Msdn Tech,.NET Development,XML and the .NET Framework...

.NET Development

Site Classified