开发者

Generating classes using XSD without base class in each one

Not sure what the appropriate tags are here...

A while back I created a batch script which, when run will convert all .xsd files found in C:\api\ into C# classes using the xsd.exe file found in the Microsoft Windows SDK (using v6.1 here).

@ECHO OFF
CLS
ECHO ***
ECHO Runs xsd.exe on all *.xsd files sorted by filename in the current folder.
ECHO ***

FOR /R "C:\api" %%G IN (*.xsd) DO (
  @ECHO ON
  "C:\Program Files\Microsoft SDKs\Windows\v6.1\Bin\xsd.exe" "C:\api\base\base.xsd" "%%G" /c /n:Mynamespace /o:"C:\api"
  @ECHO OFF
)

The problem is this... I have 50 or so generated .cs files, including the base class. And inside every single one, it generates a copy of the base class, so I end up with the base class in e开发者_运维问答very class, when I only need it in one.

Is there a way of preventing the class being generated without the base class in each one? I still want the base class to be created (base.cs), but just not in the other 49 classes.

Edit:

I have tried the following:

@ECHO OFF
SET str1=
FOR /R "C:\api" %%G IN (*.xsd) DO (
  SET str1="%%G" %str1%
)

ECHO %str1%

The response returned is always the last file in the list.


You can pass multiple XML schema files to xsd.exe at once instead of looping through the files and passing each file separately. The list of schema files being passed would not be dynamic though...

Also this creates only one .cs file, named after the first XML schema file in the list, containing all classes.


For your second problem, you have to set delayed expansion.

And use !str! instead of %str%

give this a try

SETLOCAL enabledelayedexpansion
@ECHO OFF
SET str1=
FOR /R "C:\api" %%G IN (*.xsd) DO (
  SET str1="%%G" !str1!
)
ECHO !str1!
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜