我有一个 xsd,我想以某种方式序列化。我可以通过以下方式实现我想要的,但问题是,xsd2code 生成了一个在任何地方都完全未使用的额外类。我做错了吗?我还缺少另一个技巧吗?
<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;
}
}
}