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

发布于 2024-08-19 05:46:21 字数 1217 浏览 4 评论 0原文

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

例如

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

<?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>

当我使用 xsd.exe 运行此文件时

xsd /c xsdfile.xsd

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

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

如何强制 xsd 工具包含这些内容?或者我应该切换到其他工具?

我在 Visual Studio 2008 中工作。

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

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."):

<?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 /c xsdfile.xsd

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

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?

I work in Visual Studio 2008.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ま昔日黯然 2024-08-26 05:46:21

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

这是我个人的观点,我很确定其他人也会有同样的看法。

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.

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

不弃不离 2024-08-26 05:46:21

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

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

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

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

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

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

愿与i 2024-08-26 05:46:21

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

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

其中 EnumTest 是您希望看到生成的枚举)。

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

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

<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>

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:

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

(where EnumTest is the enum you want to see generated).

The abstract attribute ensures that the element cannot play the role of the document element of an instance document. The block attribute is less important.

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>
一袭水袖舞倾城 2024-08-26 05:46:21

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

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

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

这就像一个魅力,并生成了所有内容,包括复杂类型。

I came across a similar problem, with xs:complexType not 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.

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

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

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

金橙橙 2024-08-26 05:46:21

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文