MakePeaceGreatAgain
MakePeaceGreatAgain

Reputation: 37000

Can´t create XSD from exe

I have a simple program that I want to create an schema from:

public class Program
{
    public Foo f = new Bar();
}
public abstract class Foo { }
public class Bar : Foo {}

The program compiles fine against .NET 4.0 (x86) on a 64bit-system. It has no external references to any other library (instead mscorlib of course). Now I want to create an XSD from that assembly using this command:

xsd pathToAssembly/ConsoleApplication1.exe

which gives me this:

Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.1]
Copyright (C) Microsoft Corporation. All rights reserved.

However I get an error:

Could not load file or assembly 'ConsoleApplication1.exe' or one of its dependencies. An attempt was made to load a program with an incorrect format.

which is usually caused by different runtimes. However as my project uses .NET 4 and XSD-tool (obviously) also, I´m confused why I can´t create the schema.

I already tried to call XSD from the directory where the assembly is located which causes the same error.

I´m using XSD from Visual Studio 2010 within a 64bit Windows 8.1.

EDIT: The path to XSD is this: C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A/Bin/NETFX 4.0 Tools/xsd.exe

Upvotes: 1

Views: 930

Answers (1)

CodeCaster
CodeCaster

Reputation: 151588

There's a 32 and a 64 bit version of Xsd.exe, and the one that's being run depends on your path variable.

For example the Windows SDK v7.0A puts the tools at these paths:

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\xsd.exe

Now when you run the either of those:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe" "ConsoleApplication1.exe"

It'll give the error shown. That's because that assembly (Xsd.exe, not ConsoleApplicaiton1.exe) is compiled against "AnyCPU" and will run in 64 bit on a 64 bit system. This will cause the error you see, because a 64 bit process can't load a 32 bit assembly.

You can circumvent this by forcing the 32-bit Xsd.exe to be x86 as well. You can do so from an elevated command prompt:

corflags "Path\To\xsd.exe" /32BITREQ+ /Force

Note that this will invalidate the signature of the assembly, but that won't affect its functionality. You'll want to do this not on the original Xsd.exe, but on a copy of it.

Now this assembly always runs as a 32-bit process and can happily load your x86 assembly.

Upvotes: 3

Related Questions