visual-studio .Net xsd.exe 工具不会生成所有类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2142197/
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-10-22 11:28:17  来源:igfitidea点击:

.Net xsd.exe tool doesn't generate all types

.netvisual-studioxsd.exe

提问by Mrchief

For some reason, MS .Net (v3.5) tool - xsd.exe doesn't generate types when they are not used inside any element.

出于某种原因,MS .Net (v3.5) 工具 - xsd.exe 在没有在任何元素中使用时不会生成类型。

e.g.

例如

XSD File (I threw in the complex element to avoid this warning - "Warning: cannot generate classes because no top-level elements with complex type were found."):

XSD 文件(我加入了 complex 元素以避免此警告 - “警告:无法生成类,因为找不到具有复杂类型的顶级元素。”):

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd"
    elementFormDefault="qualified"
    xmlns="http://tempuri.org/XMLSchema.xsd"
    xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
  <xs:simpleType name="EnumTest">
    <xs:restriction base="xs:string">
      <xs:enumeration value="item1" />
      <xs:enumeration value="item2" />
      <xs:enumeration value="item3" />
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="myComplexType">
    <xs:attribute name="Name" use="required" type="xs:string"/>
  </xs:complexType>
  <xs:element name="myElem" type="myComplexType"></xs:element>
</xs:schema>

When i run this thru xsd.exe using

当我使用 xsd.exe 运行此程序时

xsd /c xsdfile.xsd

xsd /c xsdfile.xsd

I don't see EnumTest in the generated cs file.

我在生成的 cs 文件中没有看到 EnumTest。

Note; Even though I don't use the enum here, but in my actual project, I have cases like this where we send enum's string value as output.

笔记; 尽管我在这里不使用枚举,但在我的实际项目中,我有这样的情况,我们将枚举的字符串值作为输出发送。

How can I force the xsd tool to include these? Or should I switch to some other tool?

如何强制 xsd 工具包含这些?还是我应该切换到其他工具?

I work in Visual Studio 2008.

我在 Visual Studio 2008 中工作。

采纳答案by Mrchief

I'll have to conclude this is a stupid shortcoming of the tool. Maybe give a switch to turn on this behavior. By not having this behavior, I'm forced to create types outside of xsd and that creates fragmented code.

我不得不得出结论,这是该工具的一个愚蠢的缺点。也许给一个开关来打开这种行为。由于没有这种行为,我被迫在 xsd 之外创建类型并创建碎片化代码。

This is my personal opinion and I'm pretty sure there are others who would share the same.

这是我个人的观点,我很确定还有其他人会分享相同的观点。

回答by Sruly

I know this is very old, but it came up in google when I was searching and I found an answer.

我知道这已经很老了,但是当我搜索并找到答案时,它出现在 google 中。

An Xsd has to have at lease one xs:element to be valid and for xsd.exe to work right.

一个 Xsd 必须至少有一个 xs:element 有效并且 xsd.exe 才能正常工作。

look at this for more info http://keithelder.net/2008/11/02/creating-a-rest-wcf-service-from-an-existing-xsd-schema/

查看更多信息 http://keithelder.net/2008/11/02/creating-a-rest-wcf-service-from-an-existing-xsd-schema/

回答by Jirka Hanika

It is possible to create enums within the XSD file even if you do not intend to use them for validation. Add the following to the XSD:

即使您不打算将它们用于验证,也可以在 XSD 文件中创建枚举。将以下内容添加到 XSD:

<xs:element name="DummyEnumTest" type="EnumTest" abstract="true" block="#all"/>

(where EnumTestis the enum you want to see generated).

EnumTest您要查看生成的枚举在哪里)。

The abstractattribute ensures that the element cannot play the role of the document element of an instance document. The blockattribute is less important.

abstract属性确保该元素不能扮演实例文档的文档元素的角色。该block属性是次要的。

There are other ways of accomplishing the same goal like declaring a prohibited attribute with your enumerated type anywhere you like. I find it useful to encapsulate all my unused enums within a generic wrapper like this, to minimize global declarations:

还有其他方法可以实现相同的目标,例如在您喜欢的任何地方使用枚举类型声明禁止属性。我发现将所有未使用的枚举封装在这样的通用包装器中很有用,以最大限度地减少全局声明:

<xs:element name="ForceGenerationOfBaseEnums" abstract="true" block="#all">
    <xs:complexType>
        <xs:choice>
            <xs:element name="..." type="..."/>
            <xs:element name="..." type="..." />
        </xs:choice>
    </xs:complexType>
</xs:element>

回答by palooka

I came across a similar problem, with xs:complexTypenot being inside an xs:element, and thus not being included in the generated cs file. In our scenario we have a wsdl file that imports two xsd files, so this might not apply to you.

我遇到了一个类似的问题,xs:complexType没有在xs:element. 中,因此没有包含在生成的 cs 文件中。在我们的场景中,我们有一个导入两个 xsd 文件的 wsdl 文件,因此这可能不适用于您。

Instead of running xsd.exe on those two xsd files, we did the following:

我们没有在这两个 xsd 文件上运行 xsd.exe,而是执行了以下操作:

wsdl.exe /language:CS /out:OutputDir OurService.wsdl first.xsd second.xsd

That worked like a charm, and generated everything, including the complex types.

这就像一个魅力,并产生了一切,包括复杂的类型。

回答by Jason M

If you don't use the enum here, or in any other class you're generating through the xsd tool, then define it in your project somewhere else just as you would any other enum. If you absolutely need to have the xsd tool create a class for you, then Workshop Alex's solution is the most commonly used workaround in this case (I don't even really consider it a workaround, its actually very convenient to be able to utilize the tool in this way)

如果您没有在此处使用枚举,或者在您通过 xsd 工具生成的任何其他类中使用枚举,则在您的项目中的其他地方定义它,就像您定义任何其他枚举一样。如果您绝对需要让 xsd 工具为您创建一个类,那么在这种情况下,Workshop Alex 的解决方案是最常用的解决方法(我什至不认为它是一种解决方法,实际上非常方便能够利用以这种方式工具)