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

dotnet xml

group:

Parsing XML Schema, How To, please


Parsing XML Schema, How To, please Mike
6/8/2005 3:25:04 PM
dotnet xml: 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"
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:element name="TemplateK86_1" type="xsd:string" minOccurs="0"
nillable="true" form="qualified"/>
</xsd:all>
</xsd:complexType>
</xsd:schema>


Thank you in advance,

Re: Parsing XML Schema, How To, please Zafar Abbas
6/9/2005 11:48:24 AM
Load the XSD in a XmlSchema object and call Compile(), you can then access
all global elements via the
Elements collection.[1]

Given an XSD file called schema.xsd the code looks like:
Code:
XmlSchema schema = XmlSchema.Read(new XmlTextReader("schema.xsd"),null);
schema.Compile();
foreach (XmlSchemaElement elemenet in schema.Elements.Values)
{
// Access the Element properties here , like QualifiedName etc.
}


[1]
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlschemaxmlschemaclasselementstopic.asp


Thanks.


[quoted text, click to view]

Re: Parsing XML Schema, How To, please Mike
6/13/2005 3:26:01 PM
Thank you very much for taking a look. Your answer brought me closer to the
solution. I tried your approach and only get the Root of the schema file.
I can not seem to be able to reach the elements.

Please let me know if you know what I do wrong.

Numerous thanks in advance,

--Michael


[quoted text, click to view]
AddThis Social Bookmark Button