Michael

Michael

  • NA
  • 38
  • 0

Detecting XmlSchemaComplexType.ContentParticleType is equal to XmlSchemaParticle.EmptyParticle

Feb 21 2009 8:06 PM

While trying to traverse an XmlSchema type in C#, I ran into the problem of trying to detect when an XmlSchemaComplexType.ContentParticleType was equal to XmlSchemaParticle.EmptyParticle.  I searched the web for an answer only to find the same question posted years ago on several sites with no solution.  I was able to determine a solution on my own.  So I am posting the original question with my solution here in hopes that it helps the next poor soul looking for an answer.

The original question is here:

*********************************************************************

> Hi

>

> I'm currently trying to create a dataset from an xml schema (.xsd).

>  (I'm unable to use XSD.exe due to the format of our xml).

>

> I have come across a problem while traversing down the schema tree.

> When I encounter an XmlSchemaComplexType I want to detect if the Particle or ContentParticleType = EmptyParticle.

> However, I am unable to do this due to EmptyParticle being an internal class to XmlSchemaParticle.

> Comparing it with null doesn't work - because the EmptyParticle is not the same as null.

>

> Can anyone point me in the right direction to get this snippet of code to work correctly ?

>

> XmlSchemaComplexType ct; // This is assigned a valid object elsewhere

>

> if (ct.Particle != XmlSchemaParticle.EmptyParticle || ct.ContentTypeParticle != null)

> {

> XmlSchemaParticle p = ct.Particle;

> if (ct.ContentTypeParticle != null)

> {

> p = ct.ContentTypeParticle;

> }

> }

> 

> Cheers

>

> --

> Ged Moretta

> Senior Software Engineer

> AppSense Ltd

*********************************************************************

Here is the solution.

It seems that in the XmlSchemaComplexType the properties ContentParticle and ContentModel are mutually exclusive.  Therefore when ContentModel is equal to null, ContentParticle is NOT equal to EmptyParticle.  Also Particle can be null.  So the code above would be changed to:

 

> if (ct.Partical != null || ct.ContentModel == null)

> {

>   XmlSchemaParticle p = ct.Particle;

>   if (ct.ContentModel == null)

>   {

>     p = ct.ContentTypeParticle;

>   }

> }