dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.12k stars 4.7k forks source link

XSD.exe does not work with .NET Core assemblies #61319

Open vsfeedback opened 2 years ago

vsfeedback commented 2 years ago

This issue has been moved from a ticket on Developer Community.


[severity:It's more difficult to complete my work] XSD.exe seems to be a .NET Framework executable. If you try to give it a .NET Core assembly to generate XSD files from, it doesn't work. (Which makes sense, because .NET Framework executables can't load .NET Core assemblies.)

In some cases, there's a workaround: Retarget your .NET Core assembly to netstandard2.0. Then XSD.exe handles it fine. I can't use this workaround, because there are non-trivial dependencies that prohibit me from retargeting to netstandard2.0.


Original Comments

Feedback Bot on 10/8/2020, 11:07 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

null on 1/18/2021, 11:53 PM:

Thank you for taking the time to log this issue! Did you still reproduce this issue in the latest stable version of Visual Studio installed from https://visualstudio.microsoft.com/downloads ?

If so, in order for us to investigate further, please provide more information by recording your repro steps via the Visual Studio Feedback Tool (Help -> Send Feedback -> Report A Problem). The feedback tool will ensure that we collect the needed information for you without worrying about what to provide (recording, dump file or ETL trace). Since this issue is now marked as Need More Info, that workflow is enabled in the Feedback Tool– full instructions
In our guidelines , you can get tips on how to provide clear and simple reproducible steps.

Get performance issues fixed quicker, please see https://docs.microsoft.com/en-us/visualstudio/ide/how-to-increase-chances-of-performance-issue-being-fixed?view=vs-2019.

We look forward to hearing from you!

William Rummler on 1/19/2021, 08:00 AM:

Yes, I can still reproduce the issue with the latest stable version of VS.

I used the Feedback Tool to record the repro steps and upload my logs, but I’m not sure how much it’ll help you, since it’s just running a console app from the SDK through a Developer Powershell window within VS. Hopefully it’s actually capturing the logging data you expect and need.

If not, it’s easy to reproduce manually. Just create a new .NET Core class library project, build it, open a VS developer prompt in that project folder, and run “xsd path\to\output\assembly.dll /t:Class1” (assuming Class1 is the default type name used by the project template).

Let me know if you need something else. Thanks.


Original Solutions

(no solutions)

ghost commented 2 years ago

Tagging subscribers to this area: @vitek-karas, @agocke, @vsadov See info in area-owners.md if you want to be subscribed.

Issue Details
_This issue has been moved from [a ticket on Developer Community](https://developercommunity2.visualstudio.com/t/XSDexe-does-not-work-with-NET-Core-ass/1210498)._ --- [severity:It's more difficult to complete my work] XSD.exe seems to be a .NET Framework executable. If you try to give it a .NET Core assembly to generate XSD files from, it doesn't work. (Which makes sense, because .NET Framework executables can't load .NET Core assemblies.) - How are we supposed to generate XSD files for types in .NET Core assemblies? In some cases, there's a workaround: Retarget your .NET Core assembly to netstandard2.0. Then XSD.exe handles it fine. I can't use this workaround, because there are non-trivial dependencies that prohibit me from retargeting to netstandard2.0. --- ### Original Comments #### Feedback Bot on 10/8/2020, 11:07 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

#### null on 1/18/2021, 11:53 PM:

Thank you for taking the time to log this issue! Did you still reproduce this issue in the latest stable version of Visual Studio installed from https://visualstudio.microsoft.com/downloads ?

If so, in order for us to investigate further, please provide more information by recording your repro steps via the Visual Studio Feedback Tool (Help -> Send Feedback -> Report A Problem). The feedback tool will ensure that we collect the needed information for you without worrying about what to provide (recording, dump file or ETL trace). Since this issue is now marked as Need More Info, that workflow is enabled in the Feedback Tool– full instructions
In our guidelines , you can get tips on how to provide clear and simple reproducible steps.

Get performance issues fixed quicker, please see https://docs.microsoft.com/en-us/visualstudio/ide/how-to-increase-chances-of-performance-issue-being-fixed?view=vs-2019.

We look forward to hearing from you!

#### William Rummler on 1/19/2021, 08:00 AM:

Yes, I can still reproduce the issue with the latest stable version of VS.

I used the Feedback Tool to record the repro steps and upload my logs, but I’m not sure how much it’ll help you, since it’s just running a console app from the SDK through a Developer Powershell window within VS. Hopefully it’s actually capturing the logging data you expect and need.

If not, it’s easy to reproduce manually. Just create a new .NET Core class library project, build it, open a VS developer prompt in that project folder, and run “xsd path\to\output\assembly.dll /t:Class1” (assuming Class1 is the default type name used by the project template).

Let me know if you need something else. Thanks.

--- ### Original Solutions (no solutions)
Author: vsfeedback
Assignees: -
Labels: `area-AssemblyLoader-coreclr`, `untriaged`
Milestone: -
ghost commented 2 years ago

Tagging subscribers to this area: @dotnet/area-system-xml See info in area-owners.md if you want to be subscribed.

Issue Details
_This issue has been moved from [a ticket on Developer Community](https://developercommunity2.visualstudio.com/t/XSDexe-does-not-work-with-NET-Core-ass/1210498)._ --- [severity:It's more difficult to complete my work] XSD.exe seems to be a .NET Framework executable. If you try to give it a .NET Core assembly to generate XSD files from, it doesn't work. (Which makes sense, because .NET Framework executables can't load .NET Core assemblies.) - How are we supposed to generate XSD files for types in .NET Core assemblies? In some cases, there's a workaround: Retarget your .NET Core assembly to netstandard2.0. Then XSD.exe handles it fine. I can't use this workaround, because there are non-trivial dependencies that prohibit me from retargeting to netstandard2.0. --- ### Original Comments #### Feedback Bot on 10/8/2020, 11:07 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.

#### null on 1/18/2021, 11:53 PM:

Thank you for taking the time to log this issue! Did you still reproduce this issue in the latest stable version of Visual Studio installed from https://visualstudio.microsoft.com/downloads ?

If so, in order for us to investigate further, please provide more information by recording your repro steps via the Visual Studio Feedback Tool (Help -> Send Feedback -> Report A Problem). The feedback tool will ensure that we collect the needed information for you without worrying about what to provide (recording, dump file or ETL trace). Since this issue is now marked as Need More Info, that workflow is enabled in the Feedback Tool– full instructions
In our guidelines , you can get tips on how to provide clear and simple reproducible steps.

Get performance issues fixed quicker, please see https://docs.microsoft.com/en-us/visualstudio/ide/how-to-increase-chances-of-performance-issue-being-fixed?view=vs-2019.

We look forward to hearing from you!

#### William Rummler on 1/19/2021, 08:00 AM:

Yes, I can still reproduce the issue with the latest stable version of VS.

I used the Feedback Tool to record the repro steps and upload my logs, but I’m not sure how much it’ll help you, since it’s just running a console app from the SDK through a Developer Powershell window within VS. Hopefully it’s actually capturing the logging data you expect and need.

If not, it’s easy to reproduce manually. Just create a new .NET Core class library project, build it, open a VS developer prompt in that project folder, and run “xsd path\to\output\assembly.dll /t:Class1” (assuming Class1 is the default type name used by the project template).

Let me know if you need something else. Thanks.

--- ### Original Solutions (no solutions)
Author: vsfeedback
Assignees: -
Labels: `area-System.Xml`, `untriaged`
Milestone: -
KalleOlaviNiemitalo commented 2 years ago

See also https://github.com/dotnet/runtime/issues/1391.

danmoseley commented 2 years ago

@HongGit maybe a dupe as noted.

danglund commented 2 years ago

Here are some minimal repro-steps that demonstrates how xsd.exe generates classes that don't work with .NET Core, but they do work with .NET Standard:

  1. Download XSD schema for German e-commerce items from: https://www.bme.de/fileadmin/content/Initativen/BMEcat/Download_BMEcat_2005/bmecat_2005_xsd.zip

  2. Using Visual Studio 2022 command line, use xsd.exe to generate C# classes from the XML Schema:

C:\Program Files\Microsoft Visual Studio\2022\Professional>xsd /c bmecat_2005_1.xsd /o:c:\temp

  1. Create a new project using .NET Core 3.1

  2. Add the class you generated in c:\temp

  3. Download a sample XML file. For example: https://github.com/stephanstapel/BMECat.net/blob/master/Documentation%20and%20Samples/WEI_BMECat_8965490000.xml

  4. Attempt do deserialize the sample XML file in code

var ser = new XmlSerializer(typeof(BMECAT));
const string filename = @"c:\temp\WEI_BMECat_8965490000.xml";
var bmeCat = ser.Deserialize(new FileStream(filename, FileMode.Open)) as BMECAT;

if (bmeCat != null) 
{
   // do whatever here...
}

The above code will work in .NET Standard. In .NET Core 3.1 you will get the error: System.PlatformNotSupportedException: 'Compiling JScript/CSharp scripts is not supported'

KalleOlaviNiemitalo commented 2 years ago

Does the code generated by xsd.exe work in .NET Core 3.1 (or .NET 6) if you also add the Microsoft.XmlSerializer.Generator package as described in Using Microsoft XML Serializer Generator on .NET Core?

danglund commented 2 years ago

Does the code generated by xsd.exe work in .NET Core 3.1 (or .NET 6) if you also add the Microsoft.XmlSerializer.Generator package as described in Using Microsoft XML Serializer Generator on .NET Core?

Thanks for the tip! I can follow the instructions and it works for the simple MyClass example in the article. The code generated by xsd.exe fails as before. It fails on runtime when it creates the XmlSerializer.

WORKS var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyClass));

FAILS var serializer = new System.Xml.Serialization.XmlSerializer(typeof(BMECAT));

The failing example warns while building the project. Something about implicit conversion between FT_VALUE and FT_VALUES.

1>.NET Xml Serialization Generation Utility, Version 6.0.0] 1>Serialization Code File Name: C:\Users\<user>\source\repos\MyApp\obj\Debug\net6.0\MyApp.XmlSerializers.cs. 1>Generated serialization code for assembly C:\Users\<user>\source\repos\MyApp\obj\Debug\net6.0\MyApp.dll --> 'C:\Users\<user>\source\repos\MyApp\obj\Debug\net6.0\MyApp.XmlSerializers.cs'. 1>obj\Debug\net6.0\MyApp.XmlSerializers.cs(8627,99): warning CS0030: Cannot convert type 'FT_VALUE' to 'FT_VALUES' 1>obj\Debug\net6.0\MyApp.XmlSerializers.cs(32184,123): warning CS0029: Cannot implicitly convert type 'FT_VALUES' to 'FT_VALUE' 1>C:\Users\<user>\.nuget\packages\microsoft.xmlserializer.generator\6.0.0\build\Microsoft.XmlSerializer.Generator.targets(53,5): warning MSB4181: The "Csc" task returned false but did not log an error. 1>C:\Users\<user>\.nuget\packages\microsoft.xmlserializer.generator\6.0.0\build\Microsoft.XmlSerializer.Generator.targets(55,5): warning : SGEN: Failed to generate the serializer for MyApp.dll. Please follow the instructions at https://go.microsoft.com/fwlink/?linkid=858594 and try again.

KalleOlaviNiemitalo commented 2 years ago

@danglund, if Microsoft.XmlSerializer.Generator generates code that fails to build, I think it's worth a separate issue because it's not a problem with xsd.exe. I searched for "CS0030" and "CS0029" in issues of this repo (where the source code is) and it looks like this error has not been reported before.

ThaDaVos commented 1 year ago

Any update on this? I tried using XsdDataContractExporter but sadly I can't get it to work with an external DLL - so really hoping that xsd.exe can be updated so I can generated xsd files from my C# classes

nalinasanka commented 1 year ago

Hi All,

I was able to generate XSD file for .Net Core library using below code. Hope it'll be helpful for all .Net Core lovers :)

try
{
    XmlSchemas schemas = new XmlSchemas();
    XmlSchemaExporter exporter = new XmlSchemaExporter(schemas);

    XmlTypeMapping mapping = new XmlReflectionImporter().ImportTypeMapping(typeof("Your Class Name"));
    exporter.ExportTypeMapping(mapping);

    using (TextWriter writer = new StreamWriter("output.xsd"))
    {
        foreach (XmlSchema schema in schemas)
        {
            schema.Write(writer);
        }
    }

    Console.WriteLine("XSD generated successfully.");
}
catch (Exception ex)
{
    Console.WriteLine($"Error in generating XSD. Error: {ex.Message}");
}

"Your Class Name" --> Needs to replace with your class name which you want to generate XSD schema.

Thanks