all groups > dotnet xml > june 2005 >
You're in the

dotnet xml

group:

Parsing XML Schema (C#)



Parsing XML Schema (C#) Mike
6/15/2005 2:20:14 PM
dotnet xml: 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_msg/1036?page=last&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,

Re: Parsing XML Schema (C#) Stan Kitsis [MSFT]
6/15/2005 4:21:52 PM
Here's an example that shows how to traverse XmlSchema object.

void Start()
{
XmlSchemaComplexType complexType;
foreach (XmlSchemaType type in xs.SchemaTypes.Values)
{
complexType = type as XmlSchemaComplexType;
if (complexType != null)
TraverseParticle(complexType.ContentTypeParticle);
}

foreach (XmlSchemaElement el in xs.Elements.Values)
TraverseParticle(el);
}

void TraverseParticle(complexType.ContentTypeParticle)
{
if (particle is XmlSchemaElement)
{
XmlSchemaElement elem = particle as XmlSchemaElement;

if (elem.RefName.IsEmpty)
{
XmlSchemaType type = (XmlSchemaType)elem.ElementSchemaType;
XmlSchemaComplexType complexType = type as XmlSchemaComplexType;
if (complexType != null && complexType.Name == null)
TraverseParticle(complexType.ContentTypeParticle);
}
}
else if (particle is XmlSchemaGroupBase)
{ //xs:all, xs:choice, xs:sequence
XmlSchemaGroupBase baseParticle = particle as XmlSchemaGroupBase;
foreach (XmlSchemaParticle subParticle in baseParticle.Items)
TraverseParticle(subParticle);
}
}

--
Stan Kitsis
Program Manager, XML Technologies
Microsoft Corporation

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm


[quoted text, click to view]

AddThis Social Bookmark Button