By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,514 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Problem with .NET Beta2 upgrade, with XSD traversal (LocalElements issue!!) :-(

P: n/a
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
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
LocalElements property was removed from .Net Framwork beta2.
You can use the following code to construct a list of total local elements
in your schema, and a list of unique local elements names.

~Zafar

private static void PrintParticles(string url) {

XmlSchema schema = XmlSchema.Read(new XmlTextReader(url, new NameTable()),
new ValidationEventHandler(ValidationCallback));
schema.Compile(new ValidationEventHandler(ValidationCallback));
ArrayList elementDeclsInContentModel = new ArrayList();
Hashtable uniqueDecls = new Hashtable();

foreach(XmlSchemaType type in schema.SchemaTypes.Values) {
XmlSchemaComplexType complexType = type as XmlSchemaComplexType;
if (complexType != null) {
elementDeclsInContentModel.Clear();
uniqueDecls.Clear();
TraverseParticle(complexType.ContentTypeParticle,
elementDeclsInContentModel, uniqueDecls);
Console.WriteLine("No of element decls: " +
elementDeclsInContentModel.Count);
Console.WriteLine("No of unique element decls: " + uniqueDecls.Count);
}
}
}

private static void TraverseParticle(XmlSchemaParticle particle, ArrayList
elementDeclsInContentModel, Hashtable uniqueDecls) {

if (particle is XmlSchemaElement) {
XmlSchemaElement elem = particle as XmlSchemaElement;
elementDeclsInContentModel.Add(elem);
if (!uniqueDecls.ContainsKey(elem.QualifiedName)) {
uniqueDecls.Add(elem.QualifiedName, elem);
}
}
else if (particle is XmlSchemaGroupBase) { //xs:all, xs:choice, xs:sequence
XmlSchemaGroupBase baseParticle = particle as XmlSchemaGroupBase;
foreach(XmlSchemaParticle subParticle in baseParticle.Items) {
TraverseParticle(subParticle, elementDeclsInContentModel, uniqueDecls);
}
}
}

"Uma Abhyankar" <au**@persistent.co.in> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
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

Nov 12 '05 #2

P: n/a
You can play with the following code:

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

"Uma Abhyankar" <au**@persistent.co.in> wrote in message
news:ut**************@TK2MSFTNGP14.phx.gbl...
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

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.