Search code examples
powershellxsd

Write a ps script that runs xsd to generate c# classes from 2 xsd files


I am trying to run this command from an powershell *.ps file:

c:\path\xsd /c ./schemas/A.xsd ./schemas/B.xsd

This command works wonders from the cli, but I am having trouble converting it to a ps script:

Set-Location .
$currentLocation = Get-Location
$xsdFilePath = "$currentLocation/schemas/A.xsd $currentLocation/schemas/B.xsd"
$outputFolder = "./destination"


& "C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\xsd.exe" $xsdFilePath /c /out:$outputFolder

Read-Host "Press Enter to exit"

The thing is that the way I am writing it, it only finds the correct path for A.xsd, but it does not find B.xsd and throws an error.

Can someone help me and explain me how to fix the issue?


Solution

  • Well, in the end I found that this modification to the script does the trick for me:

    $xsdTool = "C:\Program Files (x86)\pathToxsdTool\xsd.exe"
    
    $xsdFilePath1 = "$($currentLocation.Path)\xsd\A.xsd"
    $xsdFilePath2 = "$($currentLocation.Path)\xsd\B.xsd"
    $outputFolder = "$($currentLocation.Path)\destination"
    
    & $xsdTool $xsdFilePath1 $xsdFilePath2 /c /out:$outputFolder
    

    As for the need of having 2 xsd source files, well it was sort of a requirement. A third party we are trying to integrate with, has provided the xsd with one xsd for the common types such as enums, etc and a second one for the classes.

    In that situation running the xsd tool on the B.xsd resulted in an error cause the resulting classes needed types, etc defined in A.xsd. Running the xsd tool as in the script above specifying the 2 xsd files solves that problem