Visual Studio 2010:IDE 中类的 XML XSD 架构

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4886031/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 13:33:54  来源:igfitidea点击:

Visual Studio 2010 : XML XSD Schema to Classes within the IDE

xmlvisual-studio-2010classxsdschema

提问by MrEyes

Whenever I have needed to create a class file from an XML XSD schema, I use the .NET xsd.exe tool and prebuild events, in VS, to ensure that my classes and schemas are in sync with each other at build time. This of course works fine, however...

每当我需要从 XML XSD 架构创建类文件时,我都会在 VS 中使用 .NET xsd.exe 工具和预构建事件来确保我的类和架构在构建时彼此同步。这当然工作正常,但是......

I have noticed that if the XSD and derived class are in the same project VS understands the relationship and visually displays the CS as a child of the XSD file in solution explorer. So I was wondering if there is any functionality in Visual Studio 2010 to perform class creation for me within the IDE rather than relying on the prebuild script?

我注意到,如果 XSD 和派生类在同一个项目中,VS 会理解这种关系,并在解决方案资源管理器中直观地将 CS 显示为 XSD 文件的子级。所以我想知道 Visual Studio 2010 中是否有任何功能可以在 IDE 中为我创建类而不是依赖于预构建脚本?

For example, I would add the XSD to the project set some options somewhere and VS takes over and creates my classes for me.

例如,我会将 XSD 添加到项目中,在某处设置一些选项,然后 VS 接管并为我创建我的类。

Does VS 2010 support anything like this? Or should I just stop wondering and continue to use xsd.exe and prebuild.

VS 2010 是否支持这样的东西?或者我应该停止思考并继续使用 xsd.exe 和预构建。

EDITI have also noticed that under the properties of the XSD file I have the option to set a Custom Tool, reading the description this seems close to what I am aiming for but I am unsure of the correct usage for this property.

编辑我还注意到,在 XSD 文件的属性下,我可以选择设置 a Custom Tool,阅读描述这似乎接近我的目标,但我不确定此属性的正确用法。

EDITAlso, if there is a solution I need this to be based on the features/functions that come as standard with VS2010. I would prefer to not use a 3rd party addon etc

编辑另外,如果有一个解决方案,我需要这个基于VS2010标准的特性/功能。我宁愿不使用第 3 方插件等

采纳答案by marc_s

Visual Studio 2010 in itself does not contain any such functionality for general consumption.

Visual Studio 2010 本身不包含任何此类供一般使用的功能。

Visual Studio has some limited support for this XSD-to-code conversion in the context of typed datasets in ADO.NET - but those are not general-purpose solutions, they are targeted at exactly that one usage scenario.

Visual Studio 在 ADO.NET 中类型化数据集的上下文中对这种 XSD 到代码的转换提供了一些有限的支持 - 但这些不是通用解决方案,它们正是针对那种使用场景。

But do check outthe XSD2Codeadd-in that does exactly that

但是做检查出来XSD2Code加载项正是这么做的

enter image description here

在此处输入图片说明

回答by user2208720

You can use the XSD.exe which is in C:\program files\Microsoft\SDKs\Windows\v7.0A\Bin\xsd.exe. follow the steps to add XSD as a menu item in VS tools menu

您可以使用 C:\program files\Microsoft\SDKs\Windows\v7.0A\Bin\xsd.exe. 按照步骤将 XSD 添加为 VS 工具菜单中的菜单项

  1. Add XSD file to your VS project
  2. Go to visual studio Tools menu
  3. Go to external tools
  4. Add
  5. Enter the Title as XSD
  6. command as the path to xsd in your system(normaly C:\program files\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe. )
  7. Arguments as $(ItemPath) /c /l:cs
  8. initial directory as $(ItemDir)
  9. check the Use output window thats it press ok
  1. 将 XSD 文件添加到您的 VS 项目
  2. 转到 Visual Studio 工具菜单
  3. 转到外部工具
  4. 添加
  5. 输入标题为 XSD
  6. 命令作为系统中 xsd 的路径(通常为C:\program files\Microsoft SDKs\Windows\v7.0A\Bin\xsd.exe. )
  7. 参数为 $(ItemPath) /c /l:cs
  8. 初始目录为 $(ItemDir)
  9. 检查使用输出窗口,它按确定

Then click the Xsd files in your solution explorer and then tools--Xsd click, this will generate the class on the same folder as the .XSD file. If you already did not add the file in step 1, you will have to add the resulting .cs file into the project by right-clicking on the project in the Solution Explorer, select Add --> Existing Item and then the .CS file generated from the .XSD should be present and can be included.

然后单击解决方案资源管理器中的 Xsd 文件,然后单击工具--Xsd,这将在与 .XSD 文件相同的文件夹中生成类。如果您尚未在步骤 1 中添加文件,则必须通过右键单击解决方案资源管理器中的项目,选择添加 --> 现有项目,然后选择 .CS 文件,将生成的 .cs 文件添加到项目中从 .XSD 生成的应该存在并且可以被包括在内。

回答by Bob Levittan

There used to be a means to do that in Visual Studio. You could right click on an xml schema and there was a choice in the context menu that said something like "generate dataset." I used to use it frequently and it worked great. For some mysterious reason, Microsoft removed that function - maybe after VS 2003? I'm not sure why it's ok to use a command line utility and that it's not ok to execute the same code from a menu, but there it is.

曾经有一种方法可以在 Visual Studio 中做到这一点。您可以右键单击一个 xml 模式,上下文菜单中有一个选项,上面写着“生成数据集”之类的内容。我曾经经常使用它,而且效果很好。出于某种神秘的原因,微软删除了这个功能——也许是在 VS 2003 之后?我不确定为什么可以使用命令行实用程序,并且不能从菜单中执行相同的代码,但确实如此。