2012-04-04 104 views
38

我很難與XSD文件。如何從類創建XSD架構?

我試圖創建一個類的XSD文件:

public enum Levels { Easy, Medium, Hard } 
public sealed class Configuration 
{ 
    public string Name { get;set; } 
    public Levels Level { get; set; } 
    public ConfigurationSpec { get;set;} 
} 

public abstract class ConfigurationSpec { } 
public class ConfigurationSpec1 
{ 
    // ... 
} 
public class ConfigurationSpec2 
{ 
    // ... 
} 

請注意,我所擁有的配置裏面的抽象類。有了這個功能,是否有可能創建XSD,如果可能的話,該怎麼做?

這個想法是將類Configuration傳遞給XSD。

+0

您可以使用免費的[XML架構定義工具(Xsd.exe)](http://msdn.microsoft.com/zh-cn/library/x6c1kb0s.aspx)。 – 2012-04-04 18:45:12

回答

26

您可以使用XSD.exe(從您的Visual Studio安裝可用。)

public sealed class Configuration 
{ 
public string Name { get; set; } 
public Levels Level { get; set; } 
public ConfigurationSpec Spec { get; set; } 
} 
public abstract class ConfigurationSpec { } 
public class ConfigurationSpec1 { } 
public class ConfigurationSpec2 { } 

結果

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="Levels" type="Levels" /> 
    <xs:simpleType name="Levels"> 
    <xs:restriction base="xs:string"> 
     <xs:enumeration value="Easy" /> 
     <xs:enumeration value="Medium" /> 
     <xs:enumeration value="Hard" /> 
    </xs:restriction> 
    </xs:simpleType> 
    <xs:element name="Configuration" nillable="true" type="Configuration" /> 
    <xs:complexType name="Configuration"> 
    <xs:sequence> 
     <xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" /> 
     <xs:element minOccurs="1" maxOccurs="1" name="Level" type="Levels" /> 
     <xs:element minOccurs="0" maxOccurs="1" name="Spec" type="ConfigurationSpec" /> 
    </xs:sequence> 
    </xs:complexType> 
    <xs:complexType name="ConfigurationSpec" abstract="true" /> 
    <xs:element name="ConfigurationSpec" nillable="true" type="ConfigurationSpec" /> 
    <xs:element name="ConfigurationSpec1" nillable="true" type="ConfigurationSpec1" /> 
    <xs:complexType name="ConfigurationSpec1" /> 
    <xs:element name="ConfigurationSpec2" nillable="true" type="ConfigurationSpec2" /> 
    <xs:complexType name="ConfigurationSpec2" /> 
</xs:schema> 

所有你需要做的就是編譯你的彙編,並與路徑運行XSD.exe你議會作爲論點。 XSD.exe /?也包含所有參數的列表。

例子:XSD.exe C:\Dev\Project1\Bin\Debug\library.dll

+1

你能告訴我你遵循了哪些步驟來生成它? – 2012-04-04 18:57:15

+0

它引發我:錯誤 - 無法加載文件或程序集'file:/// C:/../test.exe'或它的一個依賴項。試圖加載格式不正確的程序。 – 2012-04-04 19:52:50

+1

@DarfZon嘗試將其更改爲與您的操作系統(x64,x86)相同的體系結構。 – Peter 2013-08-02 09:08:09

57

可以成功整合xsd.exe到Visual Studio IDE這樣的:

進入Tools, External Tools並單擊添加按鈕:

enter image description here

二千零十七分之二千零一十五

enter image description here

標題:

創建模式從階級

命令(每框架):

4.0

C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64\xsd.exe

4.5.1

C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\x64\xsd.exe

4.6。*

C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.* Tools\x64\xsd.exe

參數:

$(BinDir)$(TargetName).dll /outputdir:$(ItemDir) /type:$(ItemFileName)

使用輸出窗口:

從彈出防止額外的命令窗口,並保持輸出的記錄,直到你清除它。可能是一個好主意。

提示輸入參數:

檢查,如果你要測試的輸出或解決;否則,請不要選中。

點擊OK

使用方法:

  1. 編譯您的項目!XSD.exe只看編譯的代碼。
  2. 點擊在Solution Explorer中類。
  3. 點擊Tools, Create Schema From Class
  4. 點擊顯示在Solution Explorer中所有文件按鈕。
  5. 查看與你班級相同的文件夾,你會看到Schema0.xsd
  6. Schema0.xsd右鍵單擊並選擇Include In Project
  7. 重命名Schema0.xsd<the name of the class>.xsd
  8. (可選)你也可以通過手工編輯這個新xsd,如果你想用這個模式在XML編輯器編輯XML文件和你沒有使用所有屬性。如果確實不需要這些屬性,您可以使用use="optional"替換use="required"以消除xml編輯器中的藍色波浪線(創建警告)。
+2

不要忘記步驟** 1在使用過程中編譯**,就像我一樣。花了我一些時間才意識到:$ – 2016-02-25 12:55:12

+1

@ R.Schreurs,強調補充。謝謝! – toddmo 2016-02-25 18:44:38

+0

這個回答很好。謝謝。你認爲可以在XSD輸出文件中添加寫在類中的註釋嗎? – theLaw 2016-11-03 09:18:56