我有一個xsd,我想以某種方式查找序列化。我可以用下面的方法實現我想要的,但問題是,xsd2code會生成一個完全未使用的額外類。我做錯了嗎?我錯過了另一個詭計嗎?如何使xsd2code創建XmlArrayAttribute和XmlArrayItemAttribute而無需創建額外的類
<xsd:schema
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" >
<xsd:element name="UITranslatorConfiguration" >
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Queries" minOccurs="0" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Queries">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="Query" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="Query">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="QueryID" type="xsd:string" />
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
XML輸出我想:
<UITranslatorConfiguration>
<Queries>
<Query QueryID="queryID1">someQueryText</Query>
<Query QueryID="queryiq2">someQueryText2</Query>
<Query QueryID="queryiq3">someQueryText3</Query>
</Queries>
<UITranslatorConfiguration>
它生成的代碼:
,這是好的:
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class UITranslatorConfiguration {
[EditorBrowsable(EditorBrowsableState.Never)]
private List<Query> queriesField;
private static System.Xml.Serialization.XmlSerializer serializer;
public UITranslatorConfiguration() {
this.queriesField = new List<Query>();
}
[System.Xml.Serialization.XmlArrayAttribute(Order=0)]
[System.Xml.Serialization.XmlArrayItemAttribute("Query", IsNullable=false)]
public List<Query> Queries {
get {
return this.queriesField;
}
set {
this.queriesField = value;
}
}
}
,這是好的:
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Query {
[EditorBrowsable(EditorBrowsableState.Never)]
private string queryIDField;
[EditorBrowsable(EditorBrowsableState.Never)]
private string valueField;
private static System.Xml.Serialization.XmlSerializer serializer;
[System.Xml.Serialization.XmlAttributeAttribute()]
public string QueryID {
get {
return this.queryIDField;
}
set {
this.queryIDField = value;
}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value {
get {
return this.valueField;
}
set {
this.valueField = value;
}
}
}
這不好。這是從哪裏來的,爲什麼?它不在任何地方使用。我如何讓xsd2code不生成這個類。
[System.CodeDom.Compiler.GeneratedCodeAttribute("Xsd2Code", "3.4.0.38968")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Queries {
[EditorBrowsable(EditorBrowsableState.Never)]
private List<Query> queryField;
private static System.Xml.Serialization.XmlSerializer serializer;
public Queries() {
this.queryField = new List<Query>();
}
[System.Xml.Serialization.XmlElementAttribute("Query", Order=0)]
public List<Query> Query {
get {
return this.queryField;
}
set {
this.queryField = value;
}
}
}
你解決了嗎? – Mightymuke
@Mightymuke我沒有。我最終在對象模型中留下了額外的類。在那裏不會傷害任何東西。我可以使用未使用的代碼,即使它膨脹了這個文件,也違背了我對最小代碼的期望。 – EbbnFlow
我與生成的代碼有類似的「問題」(例如,字段名稱等)。我最終創建了漂亮而乾淨的實體類,並使用[AutoMapper](https://github.com/AutoMapper/AutoMapper)來複制數據。這意味着我不必處理生成的類,並且還提供了[反腐敗層](http://www.markhneedham。COM /博客/ 2009/07/07 /領域驅動設計,反腐敗層/)。不過,如果我找到有趣的東西,我會快速瀏覽一下xsd2code並更新它。 – Mightymuke