Groups | Blog | Home
all groups > dotnet xml > june 2005 >

dotnet xml : Finding nested elements in XSD


SideByEach
6/14/2005 7:47:20 AM
I've got a schema loaded into a XmlSchema class. Via the elements
collection I can see all the elements defiend at the root of the
schema. How can I get the definition of a nested schema element?

Thanks.
Stan Kitsis [MSFT]
6/14/2005 12:51:10 PM
Here's an example that shows how to traverse XmlSchema object.

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)
{
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);
}
}


--
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


[quoted text, click to view]

SideByEach
6/15/2005 5:15:55 AM
A thanks for the quick response. I think I get the gist of your code,
but I've never written any C# before so it's a little criptic. Do you
have a VB.Net example?
Stan Kitsis [MSFT]
6/15/2005 4:41:15 PM
I don't have a VB example, but you can probably use one of the C# to VB
converters to get the code.

--
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

[quoted text, click to view]

AddThis Social Bookmark Button