473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parsing XML Schema (C#)

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
Nov 12 '05 #1
1 23936
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
"Mike" <Mi**@discussions.microsoft.com> wrote in message
news:F1**********************************@microsof t.com...
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

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ewan B | last post by:
Hi, I'm using Xerces to parse XML files using SAX2, and am wondering if there is any information as to what exceptions are being thrown when certain validation errors occur. Taking a simple...
2
by: Christophe Vanfleteren | last post by:
Hello, I'm parsing xml that is returned by the Amazon webservices (using their REST interface). Their dev-heavy.xsd has the following entry: <xs:element name="Track"> <xs:complexType>...
4
by: Pushya | last post by:
Hello - I am trying to parse a .XSD file to obtain information about element name and type in the file. I have been trying to find some kind of documentation for Schema Parsing. All I come...
2
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home...
0
by: Pentti | last post by:
Can anyone help to understand why re-parsing occurs on a remote database (using database links), even though we are using a prepared statement on the local database: Scenario: ======== We...
2
by: Mike | last post by:
Hi! I have an Excel 2003 Schema I need to parse to extract elements names. I am puzzled which System.Xml object can help me. Here's the example of my schema: I need to get a collection of...
8
by: pradeepsarathy | last post by:
Hi all, Does the SAX parser has eventhandlers for parsing xml schema. Can we parse the xml schema the same way as we parse the xml document using SAX Parser. Thanks in advance. -pradeep
1
by: christian.eickhoff | last post by:
Hi, I have a general question regarding the parsing of XSD files, referenced within the namespace using Apache Xerces 2.7.0 (CDT). My current c++ application can parse and validate an XML file...
1
by: padmagvs | last post by:
I am working on some code which parses wsdl . I have a complex wsdl which is failing to parse . I have to modify this wsdl for parsing . wanted to know the complex wsdl i am using is as per...
1
by: JJadhav | last post by:
I have written the following schema file: <schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://www.abc.com/DDSchema" targetNamespace="http://www.abc.com/DDSchema"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.