Bug in GeneratorExtensions.SearchElementInSchema

Mar 6, 2009 at 5:38 PM
XMLSchemas can contain cyclic references.
So Schema A includes schema B and vice versa. This will cause StackOverFlowException in recursive SearchElementInSchema.

Solution.

Change SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema) to
SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema, List<string> visitedSchemaLocations)

private static XmlSchemaElement SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema,List<string> visitedSchemaLocations)
        {
          ............
            // If not found search in schema inclusion
            foreach (XmlSchemaObject item in schema.Includes)
            {
                XmlSchemaInclude schemaInc = item as XmlSchemaInclude;
                if ( (schemaInc != null) && (!visitedSchemaLocations.Exists(loc => loc == schemaInc.SchemaLocation)))  //avoid to follow cyclic refrence
                {
                    visitedSchemaLocations.Add(schemaInc.SchemaLocation); //avoid to follow cyclic refrence
                    XmlSchemaElement includeElmts = SearchElementInSchema(type, schemaInc.Schema,schemaLocations);
                    visitedSchemaLocations.Remove(schemaInc.SchemaLocation);
                    if (includeElmts != null)
                    {
                        return includeElmts;
                    }
                }
            }

            return null;
        }

Coordinator
Mar 14, 2009 at 4:45 PM

Thank you to have fixed this issue.

I’ll include your code in next release.

Pascal.

De : tmanthey [mailto:notifications@codeplex.com]
Envoyé : vendredi 6 mars 2009 19:39
À : pascal.cabanel@free.fr
Objet : Bug in GeneratorExtensions.SearchElementInSchema [Xsd2Code:49451]

From: tmanthey

XMLSchemas can contain cyclic references.
So Schema A includes schema B and vice versa. This will cause StackOverFlowException in recursive SearchElementInSchema.

Solution.

Change SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema) to
SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema, List<string> visitedSchemaLocations)

private static XmlSchemaElement SearchElementInSchema(CodeTypeDeclaration type, XmlSchema schema,List<string> visitedSchemaLocations)
{
............
// If not found search in schema inclusion
foreach (XmlSchemaObject item in schema.Includes)
{
XmlSchemaInclude schemaInc = item as XmlSchemaInclude;
if ( (schemaInc != null) && (!visitedSchemaLocations.Exists(loc => loc == schemaInc.SchemaLocation))) //avoid to follow cyclic refrence
{
visitedSchemaLocations.Add(schemaInc.SchemaLocation); //avoid to follow cyclic refrence
XmlSchemaElement includeElmts = SearchElementInSchema(type, schemaInc.Schema,schemaLocations);
visitedSchemaLocations.Remove(schemaInc.SchemaLocation);
if (includeElmts != null)
{
return includeElmts;
}
}
}

return null;
}

Read the full discussion online.

To add a post to this discussion, reply to this email (Xsd2Code@discussions.codeplex.com)

To start a new discussion for this project, email Xsd2Code@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com