data-loss 来自使用 auto-generated 序列化程序

data-loss from using auto-generated serializer

我有一个描述小模式的 xsd 文件。我在 xsd.exe 的帮助下创建了一个 C# 文件(我在开发人员命令提示符中 运行 这个命令:xsd.exe smallSchema.xsd /classes /language:CS)以便能够轻松地(反)序列化它。我获取了一个符合该模式的 xml 文件,并尝试使用生成的代码对其进行反序列化。但是我注意到发生了数据丢失!

谁能告诉我数据丢失的原因?

这是我要反序列化的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<top-element>
    <complex-elem type="plain-number">1</complex-elem>
    <top-elem-name>myTopElement</top-elem-name>
</top-element>

以下代码将反序列化,然后再次序列化并覆盖文件:

XmlSerializer serializer = new XmlSerializer(typeof(topelement));
string path = ... // path of the file on my disk
topelement rawData = null;
using (FileStream reader = new FileStream(path, FileMode.Open))
{
    rawData = (topelement)serializer.Deserialize(reader);
}
XmlSerializerNamespaces noNamespace
    = new XmlSerializerNamespaces(new XmlQualifiedName[] { new XmlQualifiedName("", "") });
    // I use it to prevent adding a namespace during serializing
using (XmlWriter wr = XmlWriter.Create(path))
{
    serializer.Serialize(wr, rawData, noNamespace);
}

但是被覆盖的文件是这样的:

<?xml version="1.0" encoding="utf-8"?>
<top-element>
    <complex-elem />
    <top-elem-name>myTopElement</top-elem-name>
</top-element>

complex-elem 中的数据丢失了!调试显示反序列化的内容已经包含 null 不应该的地方(见图)。


这是 xsd 架构:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="top-element">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="complex-elem" />
        <xs:element ref="top-elem-name" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>

  <xs:element name="complex-elem">
    <xs:complexType>
      <xs:choice>
        <xs:element ref="plain-number" />
      </xs:choice>
    </xs:complexType>
  </xs:element>

  <xs:element name="plain-number" type="xs:string" />
  <xs:element name="top-elem-name" type="xs:string" />

</xs:schema>

这里是调用 xsd.exe 的结果(注释是德语):

//------------------------------------------------------------------------------
// <auto-generated>
//     Dieser Code wurde von einem Tool generiert.
//     Laufzeitversion:4.0.30319.42000
//
//     Änderungen an dieser Datei können falsches Verhalten verursachen und gehen verloren, wenn
//     der Code erneut generiert wird.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// Dieser Quellcode wurde automatisch generiert von xsd, Version=4.6.1055.0.
// 


/// <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)]
[System.Xml.Serialization.XmlRootAttribute("top-element", Namespace="", IsNullable=false)]
public partial class topelement {

    private complexelem complexelemField;

    private string topelemnameField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("complex-elem")]
    public complexelem complexelem {
        get {
            return this.complexelemField;
        }
        set {
            this.complexelemField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("top-elem-name")]
    public string topelemname {
        get {
            return this.topelemnameField;
        }
        set {
            this.topelemnameField = value;
        }
    }
}

/// <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)]
[System.Xml.Serialization.XmlRootAttribute("complex-elem", Namespace="", IsNullable=false)]
public partial class complexelem {

    private string itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("plain-number")]
    public string Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }
}

您尝试反序列化的 XML 示例未根据您提供的 XSD 架构进行验证。

如果您尝试验证 XML,您将收到三个验证错误:

  • Cvc-complex-type.3.2.2: Attribute 'type' Is Not Allowed To Appear In Element 'complex-elem'., Line '2', Column '39'.
  • Cvc-complex-type.2.3: Element 'complex-elem' Cannot Have Character [children], Because The Type's Content Type Is Element-only., Line '2', Column '55'.
  • Cvc-complex-type.2.4.b: The Content Of Element 'complex-elem' Is Not Complete. One Of '{plain-number}' Is Expected., Line '2', Column '55'.

因此,要么将您的 XML 更改为以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<top-element>
    <complex-elem>
        <plain-number>1</plain-number>
    </complex-elem>
    <top-elem-name>myTopElement</top-elem-name>
</top-element>

... 或将您的 complex-elem 架构定义更改为此:

<xs:element name="complex-elem">
    <xs:complexType>
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute name="type" type="xs:string" />
            </xs:extension>
       </xs:simpleContent>
   </xs:complexType>
</xs:element>