public virtual CodeGenerationResults Generate(IArtifactLink link) { Guard.ArgumentNotNull(link, "link"); CodeGenerationResults content = new CodeGenerationResults(); try { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(UseXmlSerializer(link), GetNamespace(link)); string xsdMoniker = Utility.GetData<string>(link, ElementDataKey); string xmlSchemaSource = XmlSchemaUtility.GetXmlSchemaSource(xsdMoniker, link); if (!string.IsNullOrEmpty(xmlSchemaSource)) { CodeCompileUnit unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); string element = new XmlSchemaElementMoniker(xsdMoniker).ElementName; UpdateUnit(unit, element, link); ThrowOnNoTypes(unit.Namespaces, element); this.assemblyReferences = GetAssemblyReferences(link, unit.ReferencedAssemblies); CodeDomProvider provider = GetCodeDomProvider(link); GenerateCode(unit, provider, content, link.ItemPath); } } catch (Exception exception) { LogErrors(exception); } return content; }
/// <summary> /// Gets the type of the base types from referenced. /// </summary> /// <param name="xsdMoniker">The XSD moniker.</param> /// <param name="link">The link.</param> /// <returns></returns> public static IList<string> GetBaseTypesFromReferencedType(string xsdMoniker, IArtifactLink link) { Guard.ArgumentNotNullOrEmptyString(xsdMoniker, "xsdMoniker"); Guard.ArgumentNotNull(link, "link"); IList<string> types = new List<string>(); string xmlSchemaSource = GetXmlSchemaSource(xsdMoniker, link); string element = new XmlSchemaElementMoniker(xsdMoniker).ElementName; // try first with DC serializer XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(false); CodeCompileUnit unit; try { unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); } catch (InvalidSerializerException) { // now try with Xml serializer generator = new XmlSchemaTypeGenerator(true); unit = generator.GenerateCodeCompileUnit(xmlSchemaSource); } foreach (CodeNamespace ns in unit.Namespaces) { foreach (CodeTypeDeclaration codeType in ns.Types) { if (codeType.Name.Equals(element, StringComparison.OrdinalIgnoreCase)) { CollectNestedTypes(codeType, types, unit, ns.Types, link); return types; } } } return types; }
private void AddNodesFromTypes(XmlSchemaTypeGenerator generator, TreeNode node, string itemPath) { foreach (System.CodeDom.CodeNamespace ns in generator.GenerateCodeCompileUnit(itemPath).Namespaces) { foreach (System.CodeDom.CodeTypeDeclaration codeType in ns.Types) { if ((codeType.IsClass || codeType.IsEnum || codeType.IsStruct) && !node.Nodes.ContainsKey(codeType.Name)) { XsdElementNode elementNode = new XsdElementNode(codeType.Name, codeType); node.Nodes.Add(elementNode); } } } // add empty node if no elem added if (node.Nodes.Count == 0) { node.Nodes.Add(new XsdEmptyNode()); } }
public void ShouldGenerateIsNullableCollectionType() { XmlSchemaTypeGenerator generator = new XmlSchemaTypeGenerator(true); CodeCompileUnit unit = generator.GenerateCodeCompileUnit( ConfigurationLoader.GetConfigurationFilePath(@"SampleData\DescriptionModel\Restriction.xsd")); Assert.AreEqual<int>(3, unit.Namespaces[0].Types.Count); foreach (CodeAttributeDeclaration attribute in unit.Namespaces[0].Types[0].CustomAttributes) { if (attribute.AttributeType.BaseType == typeof(XmlRootAttribute).FullName) { foreach (CodeAttributeArgument argument in attribute.Arguments) { if (argument.Name == "IsNullable") { Assert.IsFalse((bool)((CodePrimitiveExpression)argument.Value).Value); return; } } } } Assert.Fail("No XmlRootAttribute or IsNullable argument found"); }