void CollectSubElements(XmlSchemaParticle particle) { // choice | sequence XmlSchemaGroupBase sgb = particle as XmlSchemaGroupBase; if (sgb != null) { foreach (XmlSchemaObject xso in sgb.Items) { XmlSchemaElement xse = xso as XmlSchemaElement; if (xse != null) { // If it's a ref, xse.QualifiedName.Name is the name of the element ElementWrapper elw = new ElementWrapper(xse, false); if (elw.IsComplexType) { Add(this.complexSubElements, xse); } else { Add(this.simpleSubElements, xse); } continue; } XmlSchemaParticle subParticle = xso as XmlSchemaParticle; if (subParticle != null) { CollectSubElements(subParticle); continue; } } } // group XmlSchemaGroupRef groupRef = particle as XmlSchemaGroupRef; if (groupRef != null) { CollectSubElements(groupRef.Particle); } }
void AnalyzeElement(XmlSchemaElement element, XmlSchemaElement parentElement) { ElementWrapper elementWrapper = new ElementWrapper(element); this.validationMessages.AddRange(elementWrapper.Warnings); // Don't process simple types if (!elementWrapper.IsComplexType) { return; } string elementTypeName = elementWrapper.TypeName; bool skip = elementWrapper.IsComplexType && elementWrapper.SimpleSubElements.Count == 0 && elementWrapper.Attributes.Count == 0 && elementWrapper.ComplexSubElements.Count < 2; bool isAlreadyGenerated = false; if (!skip) { DataTable dt; if (generatedDataSet.Tables.Contains(elementTypeName)) { dt = generatedDataSet.Tables[elementTypeName]; isAlreadyGenerated = true; } else { dt = new DataTable(elementTypeName); if (!string.IsNullOrEmpty(elementWrapper.Summary)) { dt.ExtendedProperties.Add("summary", elementWrapper.Summary); } generatedDataSet.Tables.Add(dt); } if (!isAlreadyGenerated) { foreach (XmlSchemaAttribute attrObj in elementWrapper.Attributes) { Type t = GetClrType(attrObj.SchemaTypeName); DataColumn dc = dt.Columns.Add(attrObj.Name, t); XmlSchemaSummary sum = new XmlSchemaSummary(attrObj); if (!string.IsNullOrEmpty(sum.Text)) { dc.ExtendedProperties.Add("summary", sum.Text); } // dc.ExtendedProperties.Add( "schemaType", "Attribute" ); } foreach (XmlSchemaElement subElement in elementWrapper.SimpleSubElements) { Type t = GetClrType(subElement); if (/* subElement == element && */ t == null) //TODO: Check all cases with subElement != element { continue; // element seemed to have an own type, but it's xs:any or something undetermined } string columnName = null; if (subElement == element) { columnName = "Value"; } else { columnName = subElement.Name; } DataColumn dc = dt.Columns.Add(columnName, t); dc.ExtendedProperties.Add("schemaType", "Element"); } } } if (isAlreadyGenerated) { return; } //---------------------- foreach (XmlSchemaElement subEl in elementWrapper.ComplexSubElements) { ElementWrapper subElementWrapper = new ElementWrapper(subEl, false); // Now add the relation if (!(skip && parentElement == null)) // otherwise we omit the root element { RelationEntry relEntry = new RelationEntry(); string ownName = this.namespaceWrapper.GetXPath(subElementWrapper.QualifiedName); if (!skip) { relEntry.ParentTable = elementTypeName; relEntry.XPath = ownName; } else { ElementWrapper parentElementWrapper = new ElementWrapper(parentElement, false); relEntry.ParentTable = parentElementWrapper.TypeName; string parentName = this.namespaceWrapper.GetXPath(elementWrapper.QualifiedName); relEntry.XPath = parentName + "/" + ownName; } relEntry.ChildTable = subElementWrapper.TypeName; this.relations.Add(relEntry); } if (subElementWrapper.TypeName == elementTypeName) { continue; // don't walk into endless recursion } AnalyzeElement(subEl, skip ? parentElement : element); } }