traversing a schemaset to arbitrary depth

D

dave.dolan

The example on MSDN for traversal of a schemaset is great if all you have are
top level elements, but I have complex schemas with several levels of complex
items. I seem to be only able to access the top level elements... what am I
missing? I'm trying to use the XML schema to represent the structure of xml
documents that are substituting for domain objects so I can avoid
serialization (and access virtual 'property' values with Xpath - again
without serialization and/or reflection) I need to use schemas here because
my web service clients still need to be able to generate the concrete domain
objects, but my service is agnostic of specific types.

Depth anyone?
 
D

dave.dolan

I was able to dig up some sample code, but it didn't compile

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) // <-- This
line is the offender
{
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);
}
}

so I actually found a way to fix it up, changing the line in question to
"void TraverseParticle(XmlSchemaParticle particle)"

That code actually does what I need here. I have added a delagate to be
called at every 'visit' in my code so I can 'do something' while I"m
traversing it at each node. So, I think the problem with the example code
was a simple copy-paste error. I know someone will run into this later at
some point so I figured I'd post it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top