【发布时间】:2017-05-26 03:51:35
【问题描述】:
我有一个定义如下的 XSD(我匿名了类型名称),你总是会得到一个 <response> 元素:
<xsd:element name="response">
<xsd:complexType>
<xsd:annotation>
<xsd:documentation>
Root element of the all response XML's
</xsd:documentation>
</xsd:annotation>
<xsd:sequence>
<xsd:choice>
<xsd:choice>
<xsd:element name="TypeA" type="TypeAType" />
<xsd:element name="TypeB" type="TypeBType" />
<xsd:element name="TypeC" type="TypeCType" />
</xsd:choice>
<xsd:element name="error" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
In case of error this will contain detailed error information
and no other sibling elements will exist.
If the request is successful this element will not exist.
</xsd:documentation>
</xsd:annotation>
</xsd:element>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
当我使用 XSD.exe (Visual 2013) 自动生成 C# 代码时,我得到了这个:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("response", Namespace="", IsNullable=false)]
public partial class response {
private object itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("TypeA", typeof(TypeAType))]
[System.Xml.Serialization.XmlElementAttribute("TypeB", typeof(TypeBType))]
[System.Xml.Serialization.XmlElementAttribute("TypeC", typeof(TypeCType))]
[System.Xml.Serialization.XmlElementAttribute("error", typeof(string))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
只有一个 Object 字段。甚至没有一个枚举/值来告诉我它对应于哪个元素/类型,我必须检查类型或强制转换。在过去,XSD.exe 生成了一些更加用户友好的东西。例如每种类型的字段TypeAType typeAField; string errorField; 等
我在这里的主要抱怨是error 只是一个字符串。所以我判断是否有错误的唯一方法是查看Item 是否为string。感觉真的很笨重。
谁能说明为什么 XSD.exe 会以这种方式与此 XSD 一起工作,以及是否有其他方法可以哄骗它。
【问题讨论】:
标签: .net visual-studio visual-studio-2013 xsd xsd.exe