Vous êtes sur la page 1sur 4

10/26/13

Parsing XSD Schema with SOM - CodeProject


10,169,108 members (59,669 online) Sign in

home help

articles

quick answers

discussions

features

community
Search for articles, questions, tips

Articles Platforms, Frameworks & Libraries .NET Framework How To

Next

Article Browse Code Stats Revisions Alternatives Comments & Discussions (13)

Parsing XSD Schema with SOM


By CodingBruce , 13 Oct 2008
Sign Up to vote 5.00 (9 votes)
Tw eet 0 Like 0

About Article
Pull metadata from a schema or generate XML mappers Type Licence Article CPOL 13 Oct 2008 24,786 749 24 times

Download source - 3.67 KB

First Posted Views Downloads Bookmarked

.NET1.1 .NET2.0 C#2.0 .NET3.0 C#3.0 .NET3.5 , +

Introduction
This article shows how to use a Schema Object Model(SOM) navigator as an arbitrary schema. This is useful when you have a schema and want to generate your own mapping or want to build a custom XML generator or XSD based code generator.

Top News
Microsoft took a big risk with Windows 8 and it's starting to backfire
Get the Insider News free each morning.

Using the Code


First read schema and "compile", this will validate the schema.
Collapse | Copy Code

Related Videos

p r i v a t es t a t i cX m l S c h e m a R e a d A n d C o m p i l e S c h e m a ( s t r i n gf i l e N a m e ) { X m l T e x t R e a d e rt r=n e wX m l T e x t R e a d e r ( f i l e N a m e , n e wN a m e T a b l e ( ) ) ; / /T h eR e a dm e t h o dw i l lt h r o we r r o r se n c o u n t e r e d / /o np a r s i n gt h es c h e m a X m l S c h e m as c h e m a=X m l S c h e m a . R e a d ( t r , n e wV a l i d a t i o n E v e n t H a n d l e r ( V a l i d a t i o n C a l l b a c k O n e ) ) ; t r . C l o s e ( ) ; X m l S c h e m a S e tx s e t=n e wX m l S c h e m a S e t ( ) ; x s e t . A d d ( s c h e m a ) ; x s e t . V a l i d a t i o n E v e n t H a n d l e r+ =n e w V a l i d a t i o n E v e n t H a n d l e r ( V a l i d a t i o n C a l l b a c k O n e ) ;

www.codeproject.com/Articles/30109/Parsing-XSD-Schema-with-SOM

1/4

10/26/13

V a l i d a t i o n E v e n t H a n d l e r ( V a l i d a t i o n C a l l b a c k O n e ) ; / /T h eC o m p i l em e t h o dw i l lt h r o we r r o r s / /e n c o u n t e r e do nc o m p i l i n gt h es c h e m a x s e t . C o m p i l e ( ) ; } r e t u r ns c h e m a ;

Parsing XSD Schema with SOM - CodeProject

Related Articles
An XML Schema Definition (XSD) Editor Parsing and Interpreting XSD Using LINQ WSDL and Schema Parser Parsing XML using a C++ wrapper for SAX2 XSD Tutorial - Part 2 of 5 Conventions & Recommendations
Collapse | Copy Code

p r i v a t es t a t i cv o i dV a l i d a t i o n C a l l b a c k O n e ( o b j e c ts e n d e r ,V a l i d a t i o n E v e n t A r g sa r g s ) { C o n s o l e . W r i t e L i n e ( " E x c e p t i o nS e v e r i t y :"+a r g s . S e v e r i t y ) ; C o n s o l e . W r i t e L i n e ( a r g s . M e s s a g e ) ; }

Start traversing at the highest level...

p r i v a t es t a t i cv o i dT r a v e r s e S O M ( s t r i n gx s f F i l e n a m e ) { X m l S c h e m ac u s t S c h e m a=R e a d A n d C o m p i l e S c h e m a ( x s f F i l e n a m e ) ; f o r e a c h( X m l S c h e m a E l e m e n te l e mi n { } } P r o c e s s E l e m e n t ( e l e m ) ; c u s t S c h e m a . E l e m e n t s . V a l u e s )

XML Schema Reader Writer Plugin for VS 2005/2008 Resources Files (.resx vs .resources) Chapter Excerpt - LINQ Quickly XML for Beginners LINQ to XSD Generate Sample XML from XSD Extract an XML Schema from a .NET 2.0 ConfigurationSection subclass for Configuration Validation & Intellisense in Visual Studio An XSD to .NET language code class generator that adds real punch to Microsoft's XSD Tool UDDI Explorer: Tool for searching web services How to Troubleshoot Schemas in BizTalk Server 2006 Validating SOAP Message against WSDL A cool utility to convert XML schemas to classes Generating Word Reports / Documents No-namespace/Chameleon Include Support for .NET 1.1 ExpandingTable with DesignTime support

Only an element can contain attributes... so it's handled in a little special way:
Collapse | Copy Code

p r i v a t es t a t i cv o i dP r o c e s s E l e m e n t ( X m l S c h e m a E l e m e n te l e m ) { C o n s o l e . W r i t e L i n e ( " E l e m e n t :{ 0 } " ,e l e m . N a m e ) ; i f( e l e m . E l e m e n t S c h e m a T y p ei sX m l S c h e m a C o m p l e x T y p e ) { X m l S c h e m a C o m p l e x T y p ec t= e l e m . E l e m e n t S c h e m a T y p ea sX m l S c h e m a C o m p l e x T y p e ; f o r e a c h( D i c t i o n a r y E n t r yo b ji nc t . A t t r i b u t e U s e s ) C o n s o l e . W r i t e L i n e ( " A t t r i b u t e :{ 0 } " , ( o b j . V a l u ea sX m l S c h e m a A t t r i b u t e ) . N a m e ) ; } P r o c e s s S c h e m a O b j e c t ( c t . C o n t e n t T y p e P a r t i c l e ) ;

Process the C h o i c e& S e q u e n c e elements generically (this handles the type casting required):
Collapse | Copy Code

p r i v a t es t a t i cv o i dP r o c e s s S e q u e n c e ( X m l S c h e m a S e q u e n c es e q u e n c e ) { C o n s o l e . W r i t e L i n e ( " S e q u e n c e " ) ; P r o c e s s I t e m C o l l e c t i o n ( s e q u e n c e . I t e m s ) ; } p r i v a t es t a t i cv o i dP r o c e s s C h o i c e ( X m l S c h e m a C h o i c ec h o i c e ) { C o n s o l e . W r i t e L i n e ( " C h o i c e " ) ; P r o c e s s I t e m C o l l e c t i o n ( c h o i c e . I t e m s ) ; }

The S c h e m a objects are processed generically, but uniformly:


Collapse | Copy Code

p r i v a t es t a t i cv o i dP r o c e s s I t e m C o l l e c t i o n ( X m l S c h e m a O b j e c t C o l l e c t i o no b j s ) { f o r e a c h( X m l S c h e m a O b j e c to b ji no b j s ) P r o c e s s S c h e m a O b j e c t ( o b j ) ; } p r i v a t es t a t i cv o i dP r o c e s s S c h e m a O b j e c t ( X m l S c h e m a O b j e c to b j ) { i f( o b ji sX m l S c h e m a E l e m e n t ) P r o c e s s E l e m e n t ( o b ja sX m l S c h e m a E l e m e n t ) ; i f( o b ji sX m l S c h e m a C h o i c e ) P r o c e s s C h o i c e ( o b ja sX m l S c h e m a C h o i c e ) ; i f( o b ji sX m l S c h e m a S e q u e n c e ) P r o c e s s S e q u e n c e ( o b ja sX m l S c h e m a S e q u e n c e ) ; }

www.codeproject.com/Articles/30109/Parsing-XSD-Schema-with-SOM

2/4

10/26/13

Parsing XSD Schema with SOM - CodeProject

Points of Interest
There's a lot of type checking and casting. That's inherent in the data model used in the SOM. It's not a strongly typed data structure. So you have to check, cast, then use. It would be an interesting exercise to generate a strongly typed XSD model, but I'll leave that for another article.

History
2008-10-10: Article created

License
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

CodingBruce
Engineer Big Company United States My professional career began as a developer fixing bugs on Microsoft Word97 and I've been fixing bad habits ever since. Now I do R&D work writing v1 line of business applications mostly in C#/.Net. I've been an avid pilot/instructor for 13+ years, I've built two airplanes and mostly fly gliders now for fun. I commute in an all-electric 1986 BMW 325 conversion. I'd like to get back to my academic roots of programming 3D analysis applications to organize complex systems. Article Top

Comments and Discussions


You must Sign In to use this message board.

www.codeproject.com/Articles/30109/Parsing-XSD-Schema-with-SOM

3/4

10/26/13

You must Sign In to use this message board.

Parsing XSD Schema with SOM - CodeProject

Search this forum Profile popups Spacing Relaxed Noise Very High Layout Normal Per page 10

Go

Update First Prev Next

LinqToXsd Update Thanks a lot! parsing XSD schema Re: parsing XSD schema Linq2Xsd makes this silly The lights go on... How to extract Description that is within annotation tag?Please help! [modified] Re: How to extract Description that is within annotation tag?Please help! [modified] [modified] Problem with nested <xs:group> Schema w/LinqToXsd
Last Visit: 31-Dec-99 18:00 General News Last Update: 26-Oct-13 1:55 Suggestion Question

CodingBruce DanielLey zinazsh CodingBruce CodingBruce Ger Hayden abhinavchowdhary

6-May-12 2:31 18-Mar-12 12:50 22-Nov-11 11:06 23-Nov-11 1:23 23-Sep-11 13:38 6-Jun-11 21:09 18-May-11 10:58

RayD

23-Sep-11 13:25

akothoor CodingBruce Refresh Bug Answer Joke

12-Apr-11 5:38 4-Dec-10 10:07 1 2 Next Rant Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Web03 | 2.7.1310025.1 | Last Updated 13 Oct 2008 Layout: fixed | fluid Article Copyright 2008 by CodingBruce Everything else Copyright CodeProject, 1999-2013 Terms of Use

www.codeproject.com/Articles/30109/Parsing-XSD-Schema-with-SOM

4/4

Vous aimerez peut-être aussi