我有一个描述小模式的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;
}
}
}
答案 0 :(得分:3)
您尝试反序列化的XML示例不会针对您提供的XSD架构进行验证。
如果您尝试验证XML,则会收到三个验证错误:
- Cvc-complex-type.3.2.2:不允许属性'type'出现在Element'complex-elem'。,Line'2',Column'39'。
- Cvc-complex-type.2.3:元素'complex-elem'不能有字符[children],因为Type的内容类型只是元素。,Line '2',列'55'。
- Cvc-complex-type.2.4.b:元素'complex-elem'的内容不完整。其中一个'{plain-number}'是预期的,'Line'2',Column '55'。
因此要么将XML更改为以下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>