C# 将XSD转换为.cs类

C# 将XSD转换为.cs类,c#,xsd,C#,Xsd,我知道这似乎是一个重复的问题,但我非常怀疑。 我目前正在制作一个Windows窗体应用程序,用户可以使用OpenFileDialog 一旦XSD被上传/选择,我希望它使用默认的开发者XSD工具从中创建一个.cs文件 但由于某些原因,它只是在记事本(?)中打开选定的XSD文件 我试着对代码进行注释以使其具有某种意义 //Filter only .xsd files ofd.Filter = "XSD|*.xsd"; if (ofd.ShowDial

我知道这似乎是一个重复的问题,但我非常怀疑。 我目前正在制作一个Windows窗体应用程序,用户可以使用
OpenFileDialog

一旦XSD被上传/选择,我希望它使用默认的开发者XSD工具从中创建一个.cs文件

但由于某些原因,它只是在记事本(?)中打开选定的XSD文件

我试着对代码进行注释以使其具有某种意义

 //Filter only .xsd files
            ofd.Filter = "XSD|*.xsd";
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                //Read file name
                string File = ofd.FileName;
                string z = ofd.InitialDirectory;
                //Start making commands for in the CMD 
                //Change directory to the folder where the Dev Command prompt is located
                string changeDirectory = @"cd C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\";
                //Open the Dev CMD
                string bat = "VsDevCmd";
                //Change folder to our test folder 
                string cd = @"cd C:\Users\Pierre\Desktop\testxsd";
                //execute xsd /c *selected file* /c is used to create the .cs file.
                string command = @"xsd /c " + File;
                //Combine the commands into 1 line.
                string x = cd + "&" + command;
                string xyz = changeDirectory + "&" + bat + "&" + x;
                //print the outcome -> When I copy paste this into CMD the .cs file is generated

                Console.WriteLine(xyz);
                ProcessStartInfo oInfo = new ProcessStartInfo(Environment.ExpandEnvironmentVariables(@"C:\WINDOWS\system32\cmd.exe"), xyz);
                oInfo.UseShellExecute = false;
                oInfo.ErrorDialog = false;
                oInfo.CreateNoWindow = true;
                oInfo.RedirectStandardOutput = true;
                try
                {
                    Process p = System.Diagnostics.Process.Start(oInfo);
                    System.IO.StreamReader oReader2 = p.StandardOutput;
                    string sRes = oReader2.ReadToEnd();
                    oReader2.Close();
                    // sRes now contains the output from xsd.exe     
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
因此,正如您在注释中看到的,当我将console.writeline(xyz)复制粘贴到CMD中时,它得到了正确的执行,并且生成了应该生成的.cs文件

但是,当我刚刚启动这段代码时,它会在记事本中打开所选的xsd。
几乎不知道会出什么问题

你走的是一条很长的全景路线,而实际上是一条很快的路线。。。正如@PatrickHofman在评论中所说的那样,直接使用
xsd

要执行此操作,请打开Visual Studio命令提示符,并在其中写入
xsd
,以查找
xsd
可执行文件的确切路径

然后从找到的路径和各种选项,即
/c
文件名开始使用
xsd
启动一个进程

using System.Diagnostics;
...
FileInfo fi = new FileInfo(ofd.FileName);
Process process = new Process();
process.StartInfo.FileName = xsdPath;
process.StartInfo.Arguments = "/c " + fi.FullName;
process.StartInfo.WorkingDirectory = fi.DirectoryName;
process.Start();
//wait for exit if needed...
process.WaitForExit();
如果由于某种原因,此操作不起作用,请在
process.Start()之前执行此操作以捕获命令的输出:


我想你应该用这个软件包来做这个()。这样,你就不必在整个过程中自找麻烦了

GitHub自述文件中的示例:

var generator = new Generator
{
    OutputFolder = outputFolder,
    Log = s => Console.Out.WriteLine(s),
    GenerateNullables = true,
    NamespaceProvider = new Dictionary<NamespaceKey, string> 
        { 
            { new NamespaceKey("http://wadl.dev.java.net/2009/02"), "Wadl" } 
        }
        .ToNamespaceProvider(new GeneratorConfiguration { NamespacePrefix = "Wadl" }.NamespaceProvider.GenerateNamespace)
};

generator.Generate(files);
var发电机=新发电机
{
OutputFolder=OutputFolder,
Log=s=>Console.Out.WriteLine,
GenerateNullables=true,
NamespaceProvider=新字典
{ 
{新名称空间键(“http://wadl.dev.java.net/2009/02“”,“Wadl”}
}
.tonNameSpaceProvider(新的GeneratorConfiguration{NamespacePrefix=“Wadl”}.NamespaceProvider.GenerateNamespace)
};
生成器。生成(文件);

不要使用
cmd
,而是直接调用
xsd
。然后分别传入对
xsd
的调用和参数。什么时候开始的@JMad@PatrickHofman不完全确定我应该如何执行XSDdirectly@LeonardoSeccia我试过了。没有更改任何内容,仍在打开记事本并生成.cs文件:(@LeonardoSeccia,但这不是解决方案。如果我将字符串粘贴到CMD中,则字符串是正确的。不要忘记转义
文件
。Process.start()应该有xsd+参数的路径?对吗?呃…
Process.StartInfo.Arguments=“/c”+fi.FullName;
我认为问题在于这是不正确的:
var xsd=@“C:\Program Files(x86)\Microsoft SDK\Windows\v10.0A\bin\NETFX 4.6.1 Tools\xsd.exe”;
我从
中得到的xsd
我必须修剪Prgram文件(x86)之间的空白吗嗯,这似乎不对,我无法编译。但是等一下,我会检查控制台。
var generator = new Generator
{
    OutputFolder = outputFolder,
    Log = s => Console.Out.WriteLine(s),
    GenerateNullables = true,
    NamespaceProvider = new Dictionary<NamespaceKey, string> 
        { 
            { new NamespaceKey("http://wadl.dev.java.net/2009/02"), "Wadl" } 
        }
        .ToNamespaceProvider(new GeneratorConfiguration { NamespacePrefix = "Wadl" }.NamespaceProvider.GenerateNamespace)
};

generator.Generate(files);