![](/img/trans.png)
[英]C# : creating classes from XSD file using XSD.exe (help with circular reference in XSD file)
[英]XSD.exe /dataset is not creating enumerations from my xsd file
我創建了一個XSD,並已在該.xsd文件上運行XSD.exe。 似乎我的限於枚舉值的簡單類型並未在輸出的.cs文件中作為枚舉生成。
例如,我的xsd看起來像這樣:
<xs:element name="ItemList" nillable="false">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="Item" type="ItemType" minOccurs="1" maxOccurs="unbounded" nillable="false">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ItemType">
<xs:sequence maxOccurs="1" minOccurs="1">
<!-- other complex types, etc... -->
</xs:sequence>
<xs:attribute name="Market" type="MarketType" use="required">
</xs:attribute>
<xs:attribute name="Category" type="CategoryType" use="required" />
</xs:complexType>
<xs:simpleType name="CategoryType">
<xs:restriction base="xs:string">
<xs:enumeration value="Mild" />
<xs:enumeration value="Hot" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="MarketType">
<xs:restriction base="xs:string">
<xs:enumeration value="Weak" />
<xs:enumeration value="Strong" />
</xs:restriction>
</xs:simpleType>
當我運行XSD.exe時,輸出的.cs文件不應該為每個簡單類型都具有xml枚舉屬性嗎? 這個鏈接說應該 。 也許我做錯了什么? 在.cs文件中的任何位置都看不到枚舉。
如果您需要更多信息,請讓我知道我能提供什么。
謝謝。
更新:
似乎我應該使用XSD.exe創建數據集(/ d開關),而應該創建類(/ c開關)。 在我設置它生成一個類之后,它可以正常工作。
我不知道您的情況如何-我將您的代碼復制到enum.xsd
並在xsd.exe
上運行xsd.exe
結果如下:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4016
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ItemList {
private ItemType[] itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ItemType[] Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ItemType {
private MarketType marketField;
private CategoryType categoryField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public MarketType Market {
get {
return this.marketField;
}
set {
this.marketField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public CategoryType Category {
get {
return this.categoryField;
}
set {
this.categoryField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
public enum MarketType {
/// <remarks/>
Weak,
/// <remarks/>
Strong,
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
public enum CategoryType {
/// <remarks/>
Mild,
/// <remarks/>
Hot,
}
我肯定有兩個枚舉CategoryType
和MarketType
。
我所做的只是將您的XSD代碼放入<xsl:schema>
標記中:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="TheParentNode" xmlns:xs="http://www.w3.org/2001/XMLSchema">
..... (inserted your code here) .......
</xs:schema>
然后在上面運行XSD.EXE:
xsd.exe enum.xsd /c
它創建了enum.cs
顯示的enum.cs
文件。
您擁有什么版本的XSD.EXE? 您正在使用什么版本的.NET?
渣
這個對我有用? 但是我必須添加一個架構元素,然后在ItemType中插入一個元素。
<xs:schema
elementFormDefault ="qualified"
targetNamespace ="urn:Jon.Stackoverflow._2009aug.Example"
xmlns:tns ="urn:Jon.Stackoverflow._2009aug.Example"
xmlns:xs ="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="ItemList" nillable="false">
<xs:complexType>
<xs:sequence minOccurs="1"
maxOccurs="1">
<xs:element name="Item"
type="tns:ItemType"
minOccurs="1"
maxOccurs="unbounded"
nillable="false">
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="ItemType">
<xs:sequence maxOccurs="1" minOccurs="1">
<xs:element type="xs:int" name="num"/>
</xs:sequence>
<xs:attribute name="Market"
type="tns:MarketType"
use="required">
</xs:attribute>
<xs:attribute name="Category" type="tns:CategoryType" use="required" />
</xs:complexType>
<xs:simpleType name="CategoryType">
<xs:restriction base="xs:string">
<xs:enumeration value="Mild" />
<xs:enumeration value="Hot" />
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="MarketType">
<xs:restriction base="xs:string">
<xs:enumeration value="Weak" />
<xs:enumeration value="Strong" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
它生成此(部分)
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Jon.Stackoverflow._2009aug.Example")]
public enum MarketType {
/// <remarks/>
Weak,
/// <remarks/>
Strong,
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Jon.Stackoverflow._2009aug.Example")]
public enum CategoryType {
/// <remarks/>
Mild,
/// <remarks/>
Hot,
}
如果架構中的任何字段未使用已定義的simpleType ,則其枚舉將不會在結果.cs文件中表示。
同樣地,如果使用,但與XS型工會 :字符串 ,它的枚舉仍然沒有在生成cs文件表示。
然而,如果模式包含與所述的simpleType一個字段是(即,在不與一些其它類型的聯合 ),它將在輸出cs文件來表示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.