我在 .xdr 文件中有一个 XML 架构。这是此文件的缩短版本(仅作为示例):
<?xml version = "1.0" encoding = "WINDOWS-1252"?>
<Schema name = "ANS.xdr"
xmlns = "urn:schemas-microsoft-com:xml-data"
xmlns:dt = "urn:schemas-microsoft-com:datatypes">
<ElementType name = "Shipment" content = "eltOnly" order = "seq" model = "closed">
<element type = "TrackingNumber" minOccurs = "0" maxOccurs = "1"/>
</ElementType>
<ElementType name = "TrackingNumber" content = "textOnly" dt:type = "string" model = "closed"/>
</Schema>
使用 xsd.exe 将此文件转换为 XSD 格式,它会生成以下文件:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="ANS.xdr" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="ANS.xdr" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Shipment">
<xs:complexType>
<xs:sequence>
<xs:element name="TrackingNumber" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
最后,我可以通过以下方式生成.cs
用于 XML 序列化的文件:
xsd.exe /c ANS.xsd
这会导致:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
//
/// <uwagi/>
[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("ANS.xdr", Namespace="", IsNullable=false)]
public partial class ANSxdr {
private ANSxdrShipment[] itemsField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute("Shipment", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public ANSxdrShipment[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class ANSxdrShipment {
private string trackingNumberField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TrackingNumber {
get {
return this.trackingNumberField;
}
set {
this.trackingNumberField = value;
}
}
}
问题是xsd.exe
从模式名称添加前缀到类名:public partial class ANSxdrShipment
where shoud be just Shipment
. 序列化后会导致错误的标签名称:
<?xml version="1.0" encoding="utf-8"?>
<ANSxdrShipment xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<TrackingNumber>PN-34-23414</TrackingNumber>
</ANSxdrShipment>
我可以手动更正,但是当有嵌入式 ElementTypes 时,问题似乎集中了。它会导致名称过长,尤其是在复杂的模式中,例如:
OpenShipmentsxdrOpenShipmentsOpenShipmentShipmentReturnNotificationDetailsReturnNotificationNotificationEMailAddress