Dot-Net

.Net xsd.exe 工具不會生成所有類型

  • May 16, 2013

出於某種原因,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 中工作。

我不得不得出結論,這是該工具的一個愚蠢的缺點。也許給一個開關來打開這個行為。由於沒有這種行為,我被迫在 xsd 之外創建類型並創建碎片程式碼。

這是我個人的觀點,我很確定還有其他人會分享同樣的觀點。

我知道這很老了,但是當我搜尋時它出現在Google中,我找到了答案。

Xsd 必須至少有一個 xs:element 才能有效並且 xsd.exe 才能正常工作。

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

引用自:https://stackoverflow.com/questions/2142197