我有一个 xsd 文件,其定义如下。当使用 xsd.exe
从 xsd 文件生成类时,枚举属性会获得一个额外的 FieldSpecified
属性,如下所示。除非设置了 FieldSpecified
属性,否则该值不会与属性值一起序列化。是否有我可以添加到 xsd 的附加属性或我可以与 xsd.exe
一起使用的标志以始终导致值被序列化?
来自 xsd
的示例:
<xs:simpleType name="adrLn">
<xs:restriction base="xs:string">
<xs:enumeration value="ST" />
<xs:enumeration value="APTN" />
</xs:restriction>
</xs:simpleType>
...
<xs:element name="AddressLine" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="AddrLineTypCd" type="adrLn" />
</xs:complexType>
</xs:element>
生成代码的示例:
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class RequestCheckIssueAddressAddressLine {
private adrLn addrLineTypCdField;
private bool addrLineTypCdFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public adrLn AddrLineTypCd {
get {
return this.addrLineTypCdField;
}
set {
this.addrLineTypCdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool AddrLineTypCdSpecified {
get {
return this.addrLineTypCdFieldSpecified;
}
set {
this.addrLineTypCdFieldSpecified = value;
}
}
}
最佳答案
没有改变行为的标志 - 它全部由 XSD 驱动。
枚举不可为空。您的属性是可选的(XSD 中 use 属性的默认值),因此需要 xxxSpecified 属性来控制相关字段(在您的例子中是 addrLineTypCdField 字段)。
既然您已经指出有可能更改 XSD,那么以下应该可以解决您的问题(使属性成为必需的):
<xs:attribute name="AddrLineTypCd" type="adrLn" use="required" />
关于c# - XSD 的属性以防止 XSD.exe FieldSpecified 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41841145/