none
Creating C# class from xmlschemaset runtime missing properties RRS feed

  • Question

  • I am trying to convert a xmlschemaset (containing 2 schema's, different namespaces; one importing the other) to a C# class. My code so far is:

    var xmlSchemaSet = GetMySchemaSet();//returns compiled schemaset with 2 schema's
    XsdDataContractImporterimporter = newXsdDataContractImporter();
    importer.Options =newImportOptions();
    importer.Options.GenerateSerializable = true;       

    importer.Options.ImportXmlType = true;
    importer.Options.CodeProvider = CodeDomProvider.CreateProvider("CSharp");

    if(importer.CanImport(xmlSchemaSet))
    {
       importer.Import(xmlSchemaSet);

    }

    var codeCompileUnit = importer.CodeCompileUnit;

    ???Why does importer.CanImport(xmlSchemaSet)) add two extra schema's to the set? And if .compile runs without validationerrors can I be sure the 'import' of the second schema is ok?

    I then use following code to actual compile the class:

    var results = csc.CompileAssemblyFromDom(parameters, compileUnit);
    if(results.Errors != null&& results.Errors.Count == 0)
    {

       CompiledAssembly = results.CompiledAssembly;
    }          

    This creates a .cs file and an assembly but unfortunatly the class is missing all the properties....

    If my code writes out the two schemas of the schema set to disk and I then run xsd.exe schema1.xsd schema2.xsd /c I et a .cs file containing al the right element.

    What am I missing/doing wrong?

    Sander


    • Edited by Saaffy Wednesday, June 26, 2019 11:17 AM
    Wednesday, June 26, 2019 11:16 AM

All replies

  • Hi Saaffy, 

    Thank you for posting here.

    According to your question, I have made a test, but I need more information to reproduce your problem.

    >>containing 2 schema's, different namespaces; one importing the other

    Could you provide some code  about method GetMySchemaSet()?

    We are waiting for your update.

    Best Regards,

    Xingyu ZHao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 27, 2019 6:26 AM
    Moderator
  • Hi Xingyu,

    Thank you for your response.

    Plaese find the code below. It gets the schema from a external web source. It returns a compiled schemaset with 2 schema's a base xsd and a datattypes xsd. The first imports the second through:

    <xs:importschemaLocation="DataTypes.xsd"namespace=http://schemas.xxxxxx.com/V1/datatypes/>

    publicXmlSchemaSet GetMySchemaSet()        { 
    using(ChannelFactory<IConfigurationService> factory = newChannelFactory<IConfigurationService>("ConfigurationServiceEndpoint")){
    IConfigurationServiceclient = factory.CreateChannel();
    XmlNode[] xmlNodes = client.GetMetadata(NodeConfiguration.Current.NodeKey,8 );
    XmlReaderSettingssettings = newXmlReaderSettings();
    MetadataSetmetadataSet = null;
    foreach(varxmlNode inxmlNodes)
       {
    XmlNodeReadernodeReader = newXmlNodeReader(xmlNode);
    metadataSet = MetadataSet.ReadFrom(XmlReader.Create(nodeReader, settings));
    }
    XmlSchemaSetxmlSchemaSet = newXmlSchemaSet();
    xmlSchemaSet.ValidationEventHandler += newValidationEventHandler(ValidationCallback);
    xmlSchemaSet.XmlResolver = newXmlUrlResolver();
    foreach(varmetadataSection inmetadataSet.MetadataSections)
       {
    if(metadataSection.Metadata isXmlSchema)   {
        xmlSchemaSet.Add((XmlSchema)metadataSection.Metadata);
    }

    }
    xmlSchemaSet.Compile();
    returnxmlSchemaSet;
    }
    }

    Monday, July 1, 2019 7:33 AM
  • Hi Saaffy, 

    Thanks for your feedback.

    I am trying to find a solution to your peoblem.

    >>Why does importer.CanImport(xmlSchemaSet)) add two extra schema's to the set? And if .compile runs without validationerrors can I be sure the 'import' of the second schema is ok?

    >>If my code writes out the two schemas of the schema set to disk and I then run xsd.exe schema1.xsd schema2.xsd /c I et a .cs file containing al the right element.

    Could you describe it more clearly?

    var results = csc.CompileAssemblyFromDom(parameters, compileUnit);

    I need more details about ‘parameters’ and ‘compileUnit’.

    Futhermore, some code about ‘IConfigurationService’ , ’ NodeConfiguration’ and ‘inmetadataSet’ are needed.

    It will help us to make a test.

    Thank you for your understanding, we are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 3, 2019 8:32 AM
    Moderator