Hi! I have an Excel 2003 Schema I need to parse to extract elements names.
I am puzzled with the System.Xml.Sche ma object. Here's the example of my
schema:
I need to get a collection of element names(TemplateB 62,TemplateC62 ......)
<?xml version='1.0' encoding='UTF-8'?>
<xsd:schema targetNamespace ="http://tempuri.org/XMLSchema.xsd"
elementFormDefa ult="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:complexTyp e name="RootType" >
<xsd:all>
<xsd:element name="TemplateB 62" type="xsd:strin g" minOccurs="0"
nillable="true" form="qualified "/>
<xsd:element name="TemplateC 62" type="xsd:strin g" minOccurs="0"
nillable="true" form="qualified "/>
<xsd:element name="TemplateD 62" type="xsd:strin g" minOccurs="0"
nillable="true" form="qualified "/>
<xsd:element name="TemplateE 62" type="xsd:strin g" 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)
XmlSchemaSequen ce seq = (XmlSchemaSeque nce)ct.ContentT ypeParticle;
Here's the code snippet:
XmlSchema myXmlSchema = XmlSchema.Read( new XmlTextReader(x sd),new
ValidationEvent Handler(ShowCom pileError));
myXmlSchema.Com pile(new ValidationEvent Handler(ShowCom pileError));
if(myXmlSchema. IsCompiled)
{
foreach (XmlSchemaEleme nt parentElement in myXmlSchema.Ele ments.Values)
{
XmlSchemaComple xType ct = parentElement.E lementType as
XmlSchemaComple xType; //Casting to complex type
if (ct != null)
{
XmlSchemaSequen ce seq =
(XmlSchemaSeque nce)ct.ContentT ypeParticle; //Assuming it’s a sequence of
elements
foreach(XmlSche maParticle p in seq.Items)
{
XmlSchemaElemen t elem = p as XmlSchemaElemen t; //Check if particle
in seq is XmlSchemaElemen t
if (elem != null)
{
Debug.WriteLine (elem.Qualified Name.ToString() );
}
}
}
}
}
What can I do to fix the casting problem ? What else can I do ?
Thank you in advance,
Michael Z