gpt4 book ai didi

c# - xsd.exe 为 XML 序列化生成奇怪的 C# 类

转载 作者:数据小太阳 更新时间:2023-10-29 02:09:32 26 4
gpt4 key购买 nike

我在 .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>

最后,我能够通过以下方式生成用于 XML 序列化的 .cs 文件: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 其中应该只是 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>

我可以只进行手动更正,但当有嵌入式 ElementType 时,问题似乎会集中起来。它会导致名称过长,尤其是在复杂的模式中,例如:

OpenShipmentsxdrOpenShipmentsOpenShipmentShipmentReturnNotificationDetailsReturnNotificationNotificationEMailAddress

最佳答案

你在序列化部分节点吗?如果你从根节点序列化,你应该得到

<?xml version="1.0" encoding="utf-8"?>
<ANSxdr xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Shipment>
<TrackingNumber>PN-34-23414</TrackingNumber>
</Shipment>
</ANSxdr>

看起来好像您只序列化 Shipment 对象。它没有标记任何属性,所以使用类名作为元素名。虽然 ANSxdr.Items 装饰有 XmlElementAttribute,它将为您提供“Shipment”元素。

编辑:顺便说一句,XSD 工具有几个替代品:

  1. XsdCodeGen Custom Tool - 这个人还解释了根据您的喜好自定义代码生成的方法
  2. Sample Code Generator

关于c# - xsd.exe 为 XML 序列化生成奇怪的 C# 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3582355/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com