Hi! I have an Excel 2003 Schema I need to parse to extract elements names.
I am puzzled with the System.Xml.Schema object. Here's the example of my
schema:
I need to get a collection of element names(TemplateB62,TemplateC62 ......)
<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://tempuri.org/XMLSchema.xsd">
<xsd:annotation>XSD Schema generated with Excel XML Toolbox</xsd:annotation>
<xsd:element name="Root" type="RootType"/>
<xsd:complexType name="RootType">
<xsd:all>
<xsd:element name="TemplateB62" type="xsd:string" minOccurs="0"
nillable="true" form="qualified"/>
<xsd:element name="TemplateC62" type="xsd:string" minOccurs="0"
nillable="true" form="qualified"/>
<xsd:element name="TemplateD62" type="xsd:string" minOccurs="0"
nillable="true" form="qualified"/>
<xsd:element name="TemplateE62" type="xsd:string" minOccurs="0"
nillable="true" form="qualified"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>
For some reason, I can only get the top element.
At this URL
(http://www.xml.com/cs/user/view/cs_m...t&x-order=date) I found
a code snippet that's suppossed to help me drill down into children nodes,
but it bombs on this line (Invalid cast)
XmlSchemaSequence seq = (XmlSchemaSequence)ct.ContentTypeParticle;
Here's the code snippet:
XmlSchema myXmlSchema = XmlSchema.Read(new XmlTextReader(xsd),new
ValidationEventHandler(ShowCompileError));
myXmlSchema.Compile(new ValidationEventHandler(ShowCompileError));
if(myXmlSchema.IsCompiled)
{
foreach (XmlSchemaElement parentElement in myXmlSchema.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)
{
Debug.WriteLine(elem.QualifiedName.ToString());
}
}
}
}
}
What can I do to fix the casting problem ? What else can I do ?
Thank you in advance,
Michael Z