473,793 Members | 2,810 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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
Nov 12 '05 #1
1 23963
Here's an example that shows how to traverse XmlSchema object.

void Start()
{
XmlSchemaComple xType complexType;
foreach (XmlSchemaType type in xs.SchemaTypes. Values)
{
complexType = type as XmlSchemaComple xType;
if (complexType != null)
TraverseParticl e(complexType.C ontentTypeParti cle);
}

foreach (XmlSchemaEleme nt el in xs.Elements.Val ues)
TraverseParticl e(el);
}

void TraverseParticl e(complexType.C ontentTypeParti cle)
{
if (particle is XmlSchemaElemen t)
{
XmlSchemaElemen t elem = particle as XmlSchemaElemen t;

if (elem.RefName.I sEmpty)
{
XmlSchemaType type = (XmlSchemaType) elem.ElementSch emaType;
XmlSchemaComple xType complexType = type as XmlSchemaComple xType;
if (complexType != null && complexType.Nam e == null)
TraverseParticl e(complexType.C ontentTypeParti cle);
}
}
else if (particle is XmlSchemaGroupB ase)
{ //xs:all, xs:choice, xs:sequence
XmlSchemaGroupB ase baseParticle = particle as XmlSchemaGroupB ase;
foreach (XmlSchemaParti cle subParticle in baseParticle.It ems)
TraverseParticl e(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**@discussio ns.microsoft.co m> wrote in message
news:F1******** *************** ***********@mic rosoft.com...
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

Nov 12 '05 #2

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

Similar topics

0
1777
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 example schema <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ...>
2
3854
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> <xs:sequence>
4
7248
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 across is documentation for validating XML files against XSD files. Is ther any way to parse the XSD file itself? Any help will be appreciated. Thanks for your time.
2
3959
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 Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
0
4131
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 have an schema (s1) on an Oracle 9i database with database links pointing to a schema (s2) on another Oracle 9i database.
2
1702
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 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"...
8
2705
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
3076
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 and an XSD schema, both located on my harddisk, against each other without any problems. I can therefore also parse the schema file and access it the same way than a common xml file. The question now is whether Xerces provides the possibility to...
1
3441
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 standards. first change done in schema section i have something defined like this <xsd:complexType name="CallTermType" abstract="true"/> which is used in later sections as <xsd:complexType name="EndOfPage">
1
3150
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" elementFormDefault="qualified" version="1.0"> <complexType name="PSType"> <element ref="tns:ProfileName" maxOccurs="unbounded"/>
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.