Hello,
I had a program in .NET Beta1, where in I was programmatically traversing
the Xml Schema using the given code snippet.
However today with migration to Beta2, I am facing a compilation issue that
LocalElements property on XmlSchemaComplexType not found. I now dont know
how to traverse XSD programmatically!!!
- I have a 4 level heirarchy XSD with a mix and match of complex elements
containing content and attributes both!
Please do help. Our current application BREAKS and I cannot go ahead with
development which is in its important phase, to be delivered by tomorrow!!
I would like to know what way I can traverse an XSD in a generic fashion as
possible, so that I could go to 1st level, 2nd level, 3rd or 4th level with
same peice of foreach code. Some documentation would reallly help. Also why
the LocalElements is not supported, I see its only supported in Compact
framework....
===========================================
XmlReader reader = new XmlTextReader(fileName);
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchema schema = XmlSchema.Read(reader, null);
XmlSchemaObjectEnumerator itr = schema.Items.GetEnumerator();
//schema is added to schemaset
schemaSet.Add(schema);
// compile schemaset : some properties are enabled post compilation
schemaSet.Compile();
// A better way to Traverse the XSD
foreach (XmlSchemaElement parentElem in schema.Elements.Values)
{
XmlSchemaComplexType ct = parentElem.ElementSchemaType as
XmlSchemaComplexType;
if (ct != null)
{
foreach (XmlSchemaElement childElem in ct.LocalElements.Values)
{
string str = childElem.Name;
XmlSchemaComplexType ct1 = childElem.ElementSchemaType as
XmlSchemaComplexType;
Type type = null;
foreach (XmlSchemaElement gChildElem in ct1.LocalElements.Values)
{
if (gChildElem != null)
{
// Inner elements
string str1 = gChildElem.Name;
XmlSchemaComplexType ct2 = gChildElem.ElementSchemaType as
XmlSchemaComplexType;
foreach (XmlSchemaElement gGChildElem in ct2.LocalElements.Values)
{
decimal occurence = gGChildElem.MaxOccurs;
}
}//if
}//foreach
}//foreach
}//if ct !=null
}//foreach
===========================================
Thanks
Uma