//public override string ImportSchemaType( // string name, // string ns, // XmlSchemaObject context, // XmlSchemas schemas, // XmlSchemaImporter importer, // CodeCompileUnit compileUnit, // CodeNamespace mainNamespace, // CodeGenerationOptions options, // CodeDomProvider codeProvider) //{ // XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType) schemas.Find(new XmlQualifiedName(name, ns), typeof(XmlSchemaSimpleType)); // return ImportSchemaType( // simpleType, // context, // schemas, // importer, // compileUnit, // mainNamespace, // options, // codeProvider); //} public override string ImportSchemaType( XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) { XmlSchemaAnnotated annotatedType = type as XmlSchemaAnnotated; if (annotatedType == null) { return(null); } if (annotatedType.Annotation == null) { return(null); } // create the comments and add them to the hash table under the namespace of the object CreateComments(annotatedType); //mainNamespace.Types. return(null); }
public override string ImportSchemaType( string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) { if (ns != "http://www.w3.org/2001/XMLSchema") { return(null); } switch (name) { case "anyURI": return("System.Uri"); case "gDay": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapDay"); case "gMonth": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapMonth"); case "gMonthDay": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapMonthDay"); case "gYear": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapYear"); case "gYearMonth": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapYearMonth"); case "duration": return("System.Runtime.Remoting.Metadata.W3cXsd2001.SoapDuration"); default: return(null); } }
public static CodeNamespace ProcessXsd(this string xsdFile, string targetNamespace) { // Load the XmlSchema and its collection. XmlSchema xsd; using (FileStream fs = new FileStream(xsdFile, FileMode.Open)) { xsd = XmlSchema.Read(fs, null); xsd.Compile(null); } XmlSchemas schemas = new XmlSchemas(); schemas.Add(xsd); // Create the importer for these schemas. XmlSchemaImporter importer = new XmlSchemaImporter(schemas); // System.CodeDom namespace for the XmlCodeExporter to put classes in. CodeNamespace ns = new CodeNamespace(targetNamespace); XmlCodeExporter exporter = new XmlCodeExporter(ns); // Iterate schema top-level elements and export code for each. foreach (XmlSchemaElement element in xsd.Elements.Values) { // Import the mapping first. XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName); // Export the code finally. exporter.ExportTypeMapping(mapping); } return(ns); }
protected override void BeginNamespace() { xmlImporter = new XmlSchemaImporter(LiteralSchemas, ClassNames); soapImporter = new SoapSchemaImporter(EncodedSchemas, ClassNames); xmlExporter = new XmlCodeExporter(CodeNamespace, null); xmlReflectionImporter = new XmlReflectionImporter(); }
/* * protected override IList GetMethodParameterAttributes(MethodInfo method, ParameterInfo paramInfo) * { * IList attrs = base.GetMethodParameterAttributes(method, paramInfo); * * // Add the XmlElementAttribute if needed * XmlMemberMapping inMemberMapping = inputMembersMapping[paramInfo.Position]; * if (inMemberMapping.Namespace != inputMembersMapping.Namespace) * { * CustomAttributeBuilderBuilder cabb = * new CustomAttributeBuilderBuilder(typeof(XmlElementAttribute)); * cabb.AddPropertyValue("Namespace", inMemberMapping.Namespace); * * attrs.Add(cabb.Build()); * } * * return attrs; * } */ #endregion #region Private Methods private void Initialize(DiscoveryClientDocumentCollection wsDocuments, string bindingName) { // Service descriptions this.wsDescriptions = new ServiceDescriptionCollection(); XmlSchemas schemas = new XmlSchemas(); foreach (DictionaryEntry entry in wsDocuments) { if (entry.Value is ServiceDescription) { this.wsDescriptions.Add((ServiceDescription)entry.Value); } if (entry.Value is XmlSchema) { schemas.Add((XmlSchema)entry.Value); } } // XmlSchemaImporter foreach (ServiceDescription serviceDescription in this.wsDescriptions) { foreach (XmlSchema schema in serviceDescription.Types.Schemas) { if (schemas[schema.TargetNamespace] == null) { schemas.Add(schema); } } } this.schemaImporter = new XmlSchemaImporter(schemas); this.wsBinding = GetWsBinding(this.wsDescriptions, bindingName); this.wsUrl = GetWsUrl(this.wsDescriptions, this.wsBinding); }
public override string ImportSchemaType(XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) { if (!m_direct) { if ((type is XmlSchemaSimpleType) && (context is XmlSchemaElement)) { XmlSchemaType basetype = ((XmlSchemaSimpleType)type).BaseXmlSchemaType; XmlQualifiedName qname = basetype.QualifiedName; if ((0 == string.CompareOrdinal(m_name, qname.Name)) && (0 == string.CompareOrdinal(m_targetNamespace, qname.Namespace))) { compileUnit.ReferencedAssemblies.AddRange(m_references); mainNamespace.Imports.AddRange(m_namespaceImports); return(m_destinationType); } } } return(null); }
protected override void ImportPartsBySchemaElement(QName qname, List <MessagePartDescription> parts, Message msg, MessagePart msgPart) { if (schema_set_cache != schema_set_in_use) { schema_set_cache = schema_set_in_use; var xss = new XmlSchemas(); foreach (XmlSchema xs in schema_set_cache.Schemas()) { xss.Add(xs); } schema_importer = new XmlSchemaImporter(xss); if (ccu.Namespaces.Count == 0) { ccu.Namespaces.Add(new CodeNamespace()); } var cns = ccu.Namespaces [0]; code_exporter = new XmlCodeExporter(cns, ccu); } var part = new MessagePartDescription(qname.Name, qname.Namespace); part.XmlSerializationImporter = this; var mbrNS = msg.ServiceDescription.TargetNamespace; var xmm = schema_importer.ImportMembersMapping(qname); code_exporter.ExportMembersMapping(xmm); // FIXME: use of ElementName is a hack! part.CodeTypeReference = new CodeTypeReference(xmm.ElementName); parts.Add(part); }
protected override void BeginNamespace() { xmlImporter = new XmlSchemaImporter(LiteralSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext); soapImporter = new SoapSchemaImporter(EncodedSchemas, base.CodeGenerationOptions, base.CodeGenerator, base.ImportContext); xmlExporter = new XmlCodeExporter(CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null); soapExporter = new SoapCodeExporter(CodeNamespace, null, base.CodeGenerator, base.CodeGenerationOptions, null); }
/// <summary> /// Performs building codedom tree objects from Xsd schema /// </summary> /// <param name="xsdContext">Holds data about XSD schema</param> /// <param name="codeDomContext">Holds codeDomTree</param> public void Execute(XsdContext xsdContext, CodeDomContext codeDomContext) { XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsdContext.XmlSchemas); // Step 1: Create code namespace xsdContext.CodeExporter = new XmlCodeExporter(codeDomContext.CodeNamespace); List <object> maps = new List <object>(); // Find out schema types of objects and add to collection foreach (XmlSchema xsd in xsdContext.XmlSchemaList) { foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values) { maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName)); } foreach (XmlSchemaElement schemaElement in xsd.Elements.Values) { maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName)); } } // export object collection to namespace. Now defined namespace will have all schema objects such as complexTypes, simpleTypes etc. foreach (XmlTypeMapping map in maps) { xsdContext.CodeExporter.ExportTypeMapping(map); } }
private QName ImportInternal(XmlSchemaSet schemas, QName qname) { if (qname.Namespace == KnownTypeCollection.MSSimpleNamespace) { //Primitive type return(qname); } if (imported_names.ContainsKey(qname)) { return(imported_names [qname]); } XmlSchemas xss = new XmlSchemas(); foreach (XmlSchema schema in schemas.Schemas()) { xss.Add(schema); } XmlSchemaImporter xsi = new XmlSchemaImporter(xss); XmlTypeMapping xtm = xsi.ImportTypeMapping(qname); ImportFromTypeMapping(xtm); return(qname); }
public static CodeNamespace Process() { CodeNamespace codeNameSpace = new CodeNamespace("MilkyWay.SolarSystem"); XmlSchemas schemas = new XmlSchemas(); XmlSchema xsd; using (var fs = new FileStream(Path.GetFileName("world.xsd"), FileMode.Open, FileAccess.Read)) { xsd = XmlSchema.Read(fs, null); xsd.Compile(null); schemas.Add(xsd); } XmlSchemaImporter schImporter = new XmlSchemaImporter(schemas); XmlCodeExporter exCode = new XmlCodeExporter(codeNameSpace); foreach (XmlSchemaElement element in xsd.Elements.Values) { var importTypeMapping = schImporter.ImportTypeMapping(element.QualifiedName); exCode.ExportTypeMapping(importTypeMapping); } return(codeNameSpace); }
/// <summary> /// Generates CodeDom model of the source code based on XML schema. /// </summary> /// <param name="schemas">A set of compiled XML schemas.</param> /// <returns>CodeDom model of the gererated source code.</returns> private CodeCompileUnit GenerateCodeDom(XmlSchemas schemas) { CodeCompileUnit codeCompileUnit = new CodeCompileUnit(); CodeNamespace @namespace = new CodeNamespace(string.Empty); codeCompileUnit.Namespaces.Add(@namespace); CodeGenerationOptions options = CodeGenerationOptions.None; XmlCodeExporter codeExporter = new XmlCodeExporter(@namespace, codeCompileUnit, this.LanguageProvider, options, null); XmlSchemaImporter schemaImporter = new XmlSchemaImporter(schemas, options, this.LanguageProvider, new ImportContext(new CodeIdentifiers(), false)); foreach (XmlSchema schema in schemas) { ImportSchemaAsClasses(schema, schemaImporter, codeExporter, @namespace); } CodeTypeDeclarationCollection types = @namespace.Types; if (types == null || types.Count == 0) { Console.WriteLine("No Classes Generated"); } CodeGenerator.ValidateIdentifiers(@namespace); return(codeCompileUnit); }
private static void ImportXmlSchema(XmlSchema schema, XmlSchemaImporter importer, XmlCodeExporter exporter) { foreach (XmlSchemaElement element in schema.Elements.Values) { exporter.ExportTypeMapping(importer.ImportTypeMapping(element.QualifiedName)); } }
public override string ImportSchemaType( string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace codeNamespace, CodeGenerationOptions options, CodeDomProvider codeGenerator) { if (IsBaseType(name, ns)) { return(base.ImportSchemaType(name, ns, context, schemas, importer, compileUnit, codeNamespace, options, codeGenerator)); } // Add the Namespace, except the first for (int i = 1; i < ImportNamespaces.Length; i++) { string _Import = ImportNamespaces[i]; codeNamespace.Imports.Add(new CodeNamespaceImport(_Import)); } return(name); }
private static void Main(string[] args) { XmlSchema rootSchema = GetSchemaFromFile("fpml-main-4-2.xsd"); var schemaSet = new List <XmlSchemaExternal>(); ExtractIncludes(rootSchema, ref schemaSet); var schemas = new XmlSchemas { rootSchema }; schemaSet.ForEach(schemaExternal => schemas.Add(GetSchemaFromFile(schemaExternal.SchemaLocation))); schemas.Compile(null, true); var xmlSchemaImporter = new XmlSchemaImporter(schemas); var codeNamespace = new CodeNamespace("Hosca.FpML4_2"); var xmlCodeExporter = new XmlCodeExporter(codeNamespace); var xmlTypeMappings = new List <XmlTypeMapping>(); foreach (XmlSchemaType schemaType in rootSchema.SchemaTypes.Values) { xmlTypeMappings.Add(xmlSchemaImporter.ImportSchemaType(schemaType.QualifiedName)); } foreach (XmlSchemaElement schemaElement in rootSchema.Elements.Values) { xmlTypeMappings.Add(xmlSchemaImporter.ImportTypeMapping(schemaElement.QualifiedName)); } xmlTypeMappings.ForEach(xmlCodeExporter.ExportTypeMapping); CodeGenerator.ValidateIdentifiers(codeNamespace); foreach (CodeTypeDeclaration codeTypeDeclaration in codeNamespace.Types) { for (int i = codeTypeDeclaration.CustomAttributes.Count - 1; i >= 0; i--) { CodeAttributeDeclaration cad = codeTypeDeclaration.CustomAttributes[i]; if (cad.Name == "System.CodeDom.Compiler.GeneratedCodeAttribute") { codeTypeDeclaration.CustomAttributes.RemoveAt(i); } } } using (var writer = new StringWriter()) { new CSharpCodeProvider().GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions()); //Console.WriteLine(writer.GetStringBuilder().ToString()); File.WriteAllText(Path.Combine(rootFolder, "FpML4_2.Generated.cs"), writer.GetStringBuilder().ToString()); } Console.ReadLine(); }
private static void GenerateForElements(XmlSchema xsd, XmlSchemaImporter importer, XmlCodeExporter exporter) { foreach (XmlSchemaElement element in xsd.Elements.Values) { XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName); exporter.ExportTypeMapping(mapping); } }
public void XsdToClassTest() { // identify the path to the xsd string xsdFileName = "Account.xsd"; string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); string xsdPath = Path.Combine(path, xsdFileName); // load the xsd XmlSchema xsd; using (FileStream stream = new FileStream(xsdPath, FileMode.Open, FileAccess.Read)) { xsd = XmlSchema.Read(stream, null); } Console.WriteLine("xsd.IsCompiled {0}", xsd.IsCompiled); XmlSchemas xsds = new XmlSchemas(); xsds.Add(xsd); xsds.Compile(null, true); XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xsds); // create the codedom CodeNamespace codeNamespace = new CodeNamespace("Generated"); XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace); List maps = new List(); foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values) { maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName)); } foreach (XmlSchemaElement schemaElement in xsd.Elements.Values) { maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName)); } foreach (XmlTypeMapping map in maps) { codeExporter.ExportTypeMapping(map); } RemoveAttributes(codeNamespace); // Check for invalid characters in identifiers CodeGenerator.ValidateIdentifiers(codeNamespace); // output the C# code CSharpCodeProvider codeProvider = new CSharpCodeProvider(); using (StringWriter writer = new StringWriter()) { codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions()); Console.WriteLine(writer.GetStringBuilder().ToString()); } Console.ReadLine(); }
public override string ImportSchemaType( XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider) { XmlSchemaSimpleType simpleType = type as XmlSchemaSimpleType; if (simpleType == null) { return(null); } string typeName = null; if (generatedTypes.TryGetValue(simpleType, out typeName)) { return(typeName); } XmlSchemaSimpleTypeRestriction restriction = simpleType.Content as XmlSchemaSimpleTypeRestriction; if (restriction == null) { return(null); } // genetate type only for xs:string restrictions if (restriction.BaseTypeName.Name != "string" || restriction.BaseTypeName.Namespace != XmlSchema.Namespace) { return(null); } // does not generate custom type if it has any enumeration facets foreach (object o in restriction.Facets) { if (o is XmlSchemaEnumerationFacet) { return(null); } } typeName = GenerateSimpleType(simpleType, compileUnit, mainNamespace, options, codeProvider); // add generated type information to the cache to avoid generating type al the time if (typeName != null) { generatedTypes[simpleType] = typeName; clrTypes.Add(typeName, typeName); } return(typeName); }
static void MapTypes(XmlSchema schema, XmlSchemaImporter importer, CodeNamespace cns, CodeCompileUnit ccu, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings) { XmlCodeExporter exporter = new XmlCodeExporter(cns, ccu, codeProvider, options, mappings); foreach (XmlSchemaElement element in schema.Elements.Values) { XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName); exporter.ExportTypeMapping(mapping); } }
XmlQualifiedName ExportStructMapping(StructMapping mapping, string ns) { if (mapping.TypeDesc.IsRoot) { needToExportRoot = true; return(XmlQualifiedName.Empty); } XmlSchemaComplexType type = (XmlSchemaComplexType)types[mapping]; if (type == null) { if (!mapping.IncludeInSchema) { throw new InvalidOperationException(Res.GetString(Res.XmlCannotIncludeInSchema, mapping.TypeDesc.Name)); } CheckForDuplicateType(mapping.TypeName, mapping.Namespace); type = new XmlSchemaComplexType(); type.Name = mapping.TypeName; types.Add(mapping, type); AddSchemaItem(type, mapping.Namespace, ns); type.IsAbstract = mapping.TypeDesc.IsAbstract; if (mapping.BaseMapping != null && mapping.BaseMapping.IncludeInSchema) { if (mapping.HasSimpleContent) { XmlSchemaSimpleContent model = new XmlSchemaSimpleContent(); XmlSchemaSimpleContentExtension extension = new XmlSchemaSimpleContentExtension(); extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace); model.Content = extension; type.ContentModel = model; } else { XmlSchemaComplexContentExtension extension = new XmlSchemaComplexContentExtension(); extension.BaseTypeName = ExportStructMapping(mapping.BaseMapping, mapping.Namespace); XmlSchemaComplexContent model = new XmlSchemaComplexContent(); model.Content = extension; model.IsMixed = XmlSchemaImporter.IsMixed((XmlSchemaComplexType)types[mapping.BaseMapping]); type.ContentModel = model; } } ExportTypeMembers(type, mapping.Members, mapping.TypeName, mapping.Namespace, mapping.HasSimpleContent); ExportDerivedMappings(mapping); if (mapping.XmlnsMember != null) { AddXmlnsAnnotation(type, mapping.XmlnsMember.Name); } } else { AddSchemaImport(mapping.Namespace, ns); } return(new XmlQualifiedName(type.Name, mapping.Namespace)); }
public void Start(string sDirIn, string sDirCppXmlOut, string sDirCppBinOut, string sDirJsBinOut, ValidationEventHandler oValidationEventHandler) { string sXsdPath = sDirIn + "xlsx-ext/" + gc_sXsd; XmlSchemaSet schemaSet = new XmlSchemaSet(); schemaSet.ValidationEventHandler += oValidationEventHandler; schemaSet.Add(null, sXsdPath); schemaSet.Compile(); XmlSchema chartSchema = null; XmlSchemas schemas = new XmlSchemas(); foreach (XmlSchema schema in schemaSet.Schemas()) { if (schema.TargetNamespace == gc_sTargetNamespace) { chartSchema = schema; schemas.Add(schema); } } if (null != chartSchema) { CodeNamespace ns = new CodeNamespace(); XmlCodeExporter exporter = new XmlCodeExporter(ns); CodeGenerationOptions generationOptions = CodeGenerationOptions.GenerateProperties; XmlSchemaImporter importer = new XmlSchemaImporter(schemas, generationOptions, new ImportContext(new CodeIdentifiers(), false)); foreach (XmlSchemaElement element in chartSchema.Elements.Values) { XmlTypeMapping mapping = importer.ImportTypeMapping(element.QualifiedName); exporter.ExportTypeMapping(mapping); } CodeGenerator.ValidateIdentifiers(ns); ////Microsoft.CSharp.CSharpCodeProvider oProvider; //// output the C# code //Microsoft.CSharp.CSharpCodeProvider codeProvider = new Microsoft.CSharp.CSharpCodeProvider(); //using (StringWriter writer = new StringWriter()) //{ // codeProvider.GenerateCodeFromNamespace(ns, writer, new CodeGeneratorOptions()); // string sCode = writer.GetStringBuilder().ToString(); //} List <GenClassPivot> aGenClasses = PreProcess(ns, chartSchema); aGenClasses = FilterClassesSlicer(aGenClasses); (new CodegenSlicerCPP()).Process(sDirCppXmlOut, aGenClasses, gc_sTargetNamespace); //(new CodegenJS()).Process(sDirJsBinOut, aGenClasses); } }
private static void GenerateForComplexTypes(XmlSchema xsd, XmlSchemaImporter importer, XmlCodeExporter exporter) { foreach (XmlSchemaObject type in xsd.SchemaTypes.Values) { XmlSchemaComplexType ct = type as XmlSchemaComplexType; if (ct != null) { XmlTypeMapping mapping = importer.ImportSchemaType(ct.QualifiedName); exporter.ExportTypeMapping(mapping); } } }
public virtual string ImportSchemaType ( XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider ) { return null; }
/// <summary> /// Imports XML type definitions as .NET classes. /// </summary> /// <param name="schema">Compiled XML schema.</param> /// <param name="schemaImporter">XML Schema Importer.</param> /// <param name="codeExporter">XML Code Exporter.</param> /// <param name="namespace">Namespace where .NET classes are generated.</param> private static void ImportSchemaAsClasses(XmlSchema schema, XmlSchemaImporter schemaImporter, XmlCodeExporter codeExporter, CodeNamespace @namespace) { foreach (XmlSchemaElement element in schema.Elements.Values) { if (!element.IsAbstract) { XmlTypeMapping xmlTypeMapping = schemaImporter.ImportTypeMapping(element.QualifiedName); codeExporter.ExportTypeMapping(xmlTypeMapping); GenerateLoadMethod(xmlTypeMapping, @namespace); } } }
public virtual string ImportAnyElement ( XmlSchemaAny any, bool mixed, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider ) { return null; }
public virtual string ImportSchemaType( XmlSchemaType type, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider ) { return(null); }
public virtual string ImportAnyElement( XmlSchemaAny any, bool mixed, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace mainNamespace, CodeGenerationOptions options, CodeDomProvider codeProvider ) { return(null); }
//<snippet1> public override string ImportSchemaType(string name, string ns, XmlSchemaObject context, XmlSchemas schemas, XmlSchemaImporter importer, CodeCompileUnit compileUnit, CodeNamespace codeNamespace, CodeGenerationOptions options, CodeDomProvider codeGenerator) { if (name.Equals("Order") && ns.Equals("http://orders/")) { compileUnit.ReferencedAssemblies.Add("Order.dll"); codeNamespace.Imports.Add (new CodeNamespaceImport("Microsoft.Samples")); return("Order"); } return(null); }
public static void GenerateCode(string xsdFilepath, string codeOutPath, string nameSpace) { FileStream stream = File.OpenRead(xsdFilepath); XmlSchema xsd = XmlSchema.Read(stream, ValidationCallbackOne); // Remove namespaceattribute from schema xsd.TargetNamespace = null; XmlSchemas xsds = new XmlSchemas { xsd }; XmlSchemaImporter imp = new XmlSchemaImporter(xsds); //imp.Extensions.Add(new CustomSchemaImporterExtension()); CodeNamespace ns = new CodeNamespace(nameSpace); XmlCodeExporter exp = new XmlCodeExporter(ns); foreach (XmlSchemaObject item in xsd.Items) { if (!(item is XmlSchemaElement)) { continue; } XmlSchemaElement xmlSchemaElement = (XmlSchemaElement)item; XmlQualifiedName xmlQualifiedName = new XmlQualifiedName(xmlSchemaElement.Name, xsd.TargetNamespace); XmlTypeMapping map = imp.ImportTypeMapping(xmlQualifiedName); exp.ExportTypeMapping(map); } // Remove all the attributes from each type in the CodeNamespace, except // System.Xml.Serialization.XmlTypeAttribute RemoveAttributes(ns); ToProperties(ns); CodeCompileUnit compileUnit = new CodeCompileUnit(); compileUnit.Namespaces.Add(ns); CSharpCodeProvider provider = new CSharpCodeProvider(); using (StreamWriter sw = new StreamWriter(codeOutPath, false)) { CodeGeneratorOptions codeGeneratorOptions = new CodeGeneratorOptions(); provider.GenerateCodeFromCompileUnit(compileUnit, sw, codeGeneratorOptions); } }
static void Main(string[] args) { var xmlSchemas = new XmlSchemas(); xmlSchemas.Add(XmlSchema.Read(File.OpenRead("contacts.xsd"), validationHandler)); xmlSchemas.Compile(validationHandler, true); XmlSchemaImporter schemaImporter = new XmlSchemaImporter(xmlSchemas); CodeNamespace codeNamespace = new CodeNamespace("CSharpGenerated"); XmlCodeExporter codeExporter = new XmlCodeExporter(codeNamespace, null, CodeGenerationOptions.None); foreach (XmlSchema xsd in xmlSchemas) { foreach (XmlSchemaElement schemaElement in xsd.Elements.Values) { Console.WriteLine($"{schemaElement.Name} is {schemaElement.GetType().Name}"); var t = schemaImporter.ImportTypeMapping(schemaElement.QualifiedName); codeExporter.ExportTypeMapping(t); } foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values) // XmlSchemaComplexType or XmlSchemaSimpleType { var t = schemaImporter.ImportSchemaType(schemaType.QualifiedName); Console.WriteLine($"{schemaType.Name} is a {schemaType.GetType().Name}"); // Generates far to many xmlroot attributes //codeExporter.ExportTypeMapping(schemaImporter.ImportSchemaType(schemaType.QualifiedName)); } } RemoveAttributes(codeNamespace); CodeGenerator.ValidateIdentifiers(codeNamespace); CSharpCodeProvider codeProvider = new CSharpCodeProvider(); CodeGeneratorOptions opts = new CodeGeneratorOptions { BlankLinesBetweenMembers = false, VerbatimOrder = true }; codeProvider.GenerateCodeFromNamespace(codeNamespace, Console.Out, opts); foreach (CodeTypeDeclaration codeType in codeNamespace.Types) { Console.WriteLine($"{codeType.Name} is a {codeType.GetType().Name}"); var t = codeType.TypeParameters; } Console.ReadLine(); }
static void Main(string[] args) { // identify the path to the xsd const string xsdFileName = @"schema.xsd"; var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var xsdPath = Path.Combine(path, xsdFileName); // load the xsd XmlSchema xsd; using (var stream = new FileStream(xsdPath, FileMode.Open, FileAccess.Read)) { xsd = XmlSchema.Read(stream, null); } var xsds = new XmlSchemas(); xsds.Add(xsd); xsds.Compile(null, true); var schemaImporter = new XmlSchemaImporter(xsds); // create the codedom var codeNamespace = new CodeNamespace("Generated"); var codeExporter = new XmlCodeExporter(codeNamespace); var maps = new List <XmlTypeMapping>(); foreach (XmlSchemaType schemaType in xsd.SchemaTypes.Values) { maps.Add(schemaImporter.ImportSchemaType(schemaType.QualifiedName)); } foreach (XmlSchemaElement schemaElement in xsd.Elements.Values) { maps.Add(schemaImporter.ImportTypeMapping(schemaElement.QualifiedName)); } foreach (var map in maps) { codeExporter.ExportTypeMapping(map); } PostProcess(codeNamespace); // Check for invalid characters in identifiers CodeGenerator.ValidateIdentifiers(codeNamespace); // output the C# code var codeProvider = new CSharpCodeProvider(); using (var writer = new StringWriter()) { codeProvider.GenerateCodeFromNamespace(codeNamespace, writer, new CodeGeneratorOptions()); Console.WriteLine(writer.GetStringBuilder().ToString()); } }
private CodeNamespace ExportCode() { XmlSchema xsd; XmlSchemas schemas; LoadSchemas(out xsd, out schemas); CodeNamespace ns = new CodeNamespace(base.FileNameSpace); XmlSchemaImporter importer = new XmlSchemaImporter(schemas); XmlCodeExporter exporter = new XmlCodeExporter(ns); GenerateForElements(xsd, importer, exporter); GenerateForComplexTypes(xsd, importer, exporter); return(ns); }