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

