【问题标题】:Generating XSD from a Class in C#从 C# 中的类生成 XSD
【发布时间】:2013-11-20 18:09:53
【问题描述】:

我正在制作一个将配置保存到 XML 文件的程序。

我目前使用 XMLserializer 从类生成 XML,反之亦然。

我还希望能够从该类生成一个 XSD 文件,以便能够验证未来的 XML 文件,包括那些未自动生成的文件。

此功能应包括为元素指定属性的选项,例如 minValue、maxLength 和默认值(可能通过使用注释)。

到目前为止,我读过的唯一工具是 XSD.exe,我不知道如何以编程方式(来自 C# 代码)使用它,也不知道它是否是一种好方法。

【问题讨论】:

  • 我也对此感兴趣。你找到解决办法了吗?
  • 没有。我看到的唯一选择是自己编写代码

标签: c# xml serialization xsd


【解决方案1】:

还有其他用于从 XSD 生成 C# 的工具(例如 XSD2Code),但我不知道他们是否有编程 API。

您始终可以使用Process.Start() 从您的 C# 应用程序中启动 XSD.exe(或任何其他工具)进程。一个简单粗暴的例子:

var proc = Process.Start(new ProcessStartInfo
    {
        FileName = "xsd.exe",
        Arguments = "/c /namespace:Your.NameSpace yourXsdFile.xsd /o:\"C:\\yourOutputDir\\\"",
        WindowStyle = ProcessWindowStyle.Hidden,
        UseShellExecute = false
    });
proc.WaitForExit();
var exitCode = proc.ExitCode;
if (exitCode == 0)
{
    // post-processing
}
else
{
    // handle errors
}

显然有一些细节需要填写,你需要 XSD.exe 在你的 PATH 中(或者,在代码中指定它的完整路径)等等,但基本的想法应该是可行的。

【讨论】:

    【解决方案2】:
     Go to your command prompt. After that go to the following path
     1.C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64>
     2. After that write 
     xsd.exe
     3. After that paste you .dll file path.
     4. run command
     5.Output: Go to the following location:(** Command prompt indicate you about such link) and you will see your desire xsd file schema
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0
    Tools\x64\
    
    Example: in CMD.exe
    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64>xsd.
    exe "E:\TFS Projects\Project\Admin\BO\bin\Debug\BO.dll"
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0
    Tools\x64\schema0.xsd'.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多