将XSD转换为.cs类

问题描述 投票:2回答:2

我知道这似乎是一个重复的问题,但我对此表示高度怀疑。我目前正在制作Windows窗体应用程序,用户可以使用OpenFileDialog选择XSD文件

上传/选择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。字面上不知道什么是错的

c# xsd
2个回答
2
投票

当你实际上有一个非常快的时候,你有点采取非常长的全景路线...正如@PatrickHofman在评论中所述,直接使用xsd ...

为此,请打开Visual Studio命令提示符,并编写where xsd以查找xsd可执行文件的确切路径。

然后从你找到的路径和各种选项中使用xsd开始一个过程。 /cfilename

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()之前执行此操作来捕获命令的输出:

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += 
(sender, args) => Console.WriteLine("received output: {0}", args.Data);
process.BeginOutputReadLine();

-1
投票

我认为你应该使用XmlSchemaClassGenerator包(Nuget)。这样你就不必自己做所有的过程。

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);
© www.soinside.com 2019 - 2024. All rights reserved.